Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert from Object[] to int[]

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);
    }
like image 387
John Joe Avatar asked Jul 29 '15 10:07

John Joe


2 Answers

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

Integer[] vec = h.toArray(new Integer[h.size()]);
like image 146
marstran Avatar answered Sep 19 '22 04:09

marstran


With Java8 this would be possible:

int[] vec1 =  h.stream().filter(t -> t != null).mapToInt(t -> t).toArray();
like image 27
Martin Avatar answered Sep 19 '22 04:09

Martin