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);
    }
Ihor Kaharlichenko
  • 5,944
  • 1
  • 26
  • 32
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • Possible duplicate of http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java – marstran Jul 29 '15 at 10:24

5 Answers5

4

With Java8 this would be possible:

int[] vec1 =  h.stream().filter(t -> t != null).mapToInt(t -> t).toArray();
Community
  • 1
  • 1
Martin
  • 680
  • 8
  • 12
  • This may throw a NullPointerException. You need to either filter out null values, or handle them in the mapping function. – marstran Jul 29 '15 at 10:19
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;
  • 1
    This will not work. Gives `java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;` – sakura Apr 12 '18 at 11:31
3

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

Integer[] vec = h.toArray(new Integer[h.size()]);
marstran
  • 26,413
  • 5
  • 61
  • 67
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".

Oleksandr_DJ
  • 1,454
  • 2
  • 14
  • 26
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()]);
AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23