how to convert from Object[] to int[]
Asked Answered
M

5

7

I want to pass myVector to another class (Case.java) but I get this kind of error message. Type mismatch: cannot convert from Object[] to int[]. Can anybody tell me how to solve this?

User.java

JButton btnNewButton = new JButton("Process");

btnNewButton.setBounds(360, 296, 89, 23);
contentPane.add(btnNewButton);

btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Integer a = (comboBox.getSelectedIndex() + 1);
        Integer b = (comboBox_1.getSelectedIndex() + 1);
        Integer day = (comboBox_2.getSelectedIndex() + 1);
        ArrayList<Integer> myVector = new ArrayList<Integer>();
        myVector.add(a);
        myVector.add(b);
        myVector.add(day);

        Case ca = new Case();
        try {
            ca.addPlace(myVector);
            LoginGUI um = new LoginGUI();
            um.setVisible(true);

        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

Case.java

public class Case {

    public Case() {
        // TODO Auto-generated constructor stub
    }

    public void addPlace(ArrayList<Integer> h) {

        int vec1[] = h.toArray();
        int vec2[] = {2, 1, 3, 2};

        double cos_sim = cosine_similarity(vec1, vec2);
    }
Meal answered 29/7, 2015 at 10:5 Comment(1)
Possible duplicate of #960931Devotional
C
4

With Java8 this would be possible:

int[] vec1 =  h.stream().filter(t -> t != null).mapToInt(t -> t).toArray();
Changteh answered 29/7, 2015 at 10:14 Comment(1)
This may throw a NullPointerException. You need to either filter out null values, or handle them in the mapping function.Devotional
L
3

We cannot convert Object[] to int[]. Those two classes are Horizontal.

If you want you can convert Object[] into Integer[] Wrapper class with Explicit Down Casting.

        Object[] obj ={10,30,20,100};
       Integer[] in = (Integer[]) obj;
Laforge answered 29/7, 2015 at 10:10 Comment(1)
This will not work. Gives java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;Gains
D
3

Pass a new array to the toArray method. Like this:

Integer[] vec = h.toArray(new Integer[h.size()]);
Devotional answered 29/7, 2015 at 10:12 Comment(1)
This is the answer.Torin
E
0

I suppose you have to perform manual conversion:

ArrayList<Integer> h = new ArrayList<>();
int[] result = new int[h.size()];
int index = 0;
for(int val:h){
    result[index++] =val; 
}

Of course, it could throw NullPointerException in case you have null-values in original ArrayList "h".

Excise answered 29/7, 2015 at 10:19 Comment(0)
E
0

First of all you are not converting Object[] to int[], what you are doing here is ArrayList<Integer> to int[]. Th error which you are getting cannot convert from Object[] to int[] is because Integer[] is not directly convertible to int[].

So this one is directly not possible with any method. But you can do it like :-

        int[] vec1= new int[h.size()];
        int index = 0;
        for(int i : h){
            vec1[index] = i;
            index++;
        }

But if you can change your int[] vec1 to Interger[] vec1 then you can do like :-

 Integer[] vec1 =  h.toArray(new Integer[h.size()]);
Effervescent answered 29/7, 2015 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.