gpt4 book ai didi

java - 从 jlistbox 中的给定值中选择最大值

转载 作者:行者123 更新时间:2023-12-02 02:32:23 24 4
gpt4 key购买 nike

enter image description here

enter image description here

我想从jListBox中已存在的值中获取最大值。我该怎么做?我已附上图片。例如列出的图像中的最大值是4536。如何从查询中获取该值?

最佳答案

你可以这样做(阅读代码中的注释):

ListModel model = jList1.getModel();   // Get the JList model...
int highest = 0; // Will hold the highest list integer value.
int desiredIndex = 0; // will hold the list index value of highest list integer value.
// Iterate through the elements in JList model.
for (int i = 0; i < model.getSize(); i++) {
String listItem = model.getElementAt(i).toString(); // place current element into a variable
// Is the current list item a numerical Value (RegEx used here)
if (listItem.matches("\\d+")) {
// Yes it is...
int val = Integer.parseInt(listItem); // Convert string numerical value to a Integer value
// Is this list value higher that what is in the highest variable?
if (val > highest) {
// Yes it is...
highest = val; // Make hiighest hold the new highest value
desiredIndex = i; // Hold the index number of where this value is located within the list.
}
}
// No it isn't...loop again.
}

// Iteration is complete...
System.out.println("Highest value is: " + highest); // Print highest value to console.
jList1.setSelectedIndex(desiredIndex); // Select the highest value within the JList.
// Done!

关于java - 从 jlistbox 中的给定值中选择最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223428/

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