how to convert from Object[] to int[]

John Joe picture John Joe · Jul 29, 2015 · Viewed 8.1k times · Source

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);
    }

Answer

Anil Reddy Yarragonda picture Anil Reddy Yarragonda · Jul 29, 2015

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;