java - 从 jlistbox 中的给定值中选择最大值
解决方案
你可以这样做(阅读代码中的注释):
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!
推荐阅读
- azure - Azure AD Graph API 返回不支持的登录活动查询
- api - (SSG-WSG) API 中的技能框架与网站显示的技能框架不符
- javascript - 替换 mongoDB 中嵌套的多个对象
- firefox - 随机 ssl 认证失败
- c++ - 比较语句
- html - HTML 和 CSS 文件在 chrome 中不起作用,错误?
- kivy - Kivy 示例代码的 Permisson Denied 错误
- python-3.x - 我正在尝试在 mac os 中安装 pandas (python3.9) 并遇到错误
- bash - 这是管道命令吗?
- python - 程序未根据设置条件打印信息