gpt4 book ai didi

java - 如何从 Object[] 转换为 int[]

转载 作者:搜寻专家 更新时间:2023-10-31 19:45:09 25 4
gpt4 key购买 nike

我想将 myVector 传递给另一个类 (Case.java),但我收到了此类错误消息。类型不匹配:无法从 Object[] 转换为 int[]。谁能告诉我如何解决这个问题?

用户.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);
}

最佳答案

使用 Java8 这将是可能的:

int[] vec1 =  h.stream().filter(t -> t != null).mapToInt(t -> t).toArray();

关于java - 如何从 Object[] 转换为 int[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31697305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com