以下是一个实现获取所有最小值索引的方法,该方法不使用 "list.get" 方法: \n\njava\npublic static List<Integer> getMinValueIndexes(List<BigDecimal> bigDecimals, List<BigDecimal> minValueList, List<Integer> minIndexList) {\n // 遍历列表,找到最小值\n for (int i = 0; i < bigDecimals.size(); i++) {\n BigDecimal current = bigDecimals.get(i);\n int comparison = current.compareTo(minValueList.get(0));\n\n // 如果当前元素小于最小值,则更新最小值,并清空已记录的索引\n if (comparison < 0) {\n minValueList.clear();\n minValueList.add(current);\n minIndexList.clear();\n minIndexList.add(i);\n } \n\n // 如果当前元素等于最小值,则记录当前索引\n if (comparison == 0) {\n minIndexList.add(i);\n }\n }\n\n return minIndexList;\n}\n\n\n该方法通过传入参数的方式获取元素,而不是使用 "list.get" 方法。在调用方法时,需要传入包含最小值列表和最小值索引列表的参数。\n\n示例:\n\njava\nList<BigDecimal> bigDecimals = Arrays.asList(new BigDecimal("1.2"), new BigDecimal("0.5"), new BigDecimal("0.5"), new BigDecimal("1.8"));\nList<BigDecimal> minValueList = new ArrayList<>();\nList<Integer> minIndexList = new ArrayList<>();\nminValueList.add(bigDecimals.get(0));\nminIndexList.add(0);\nList<Integer> indexes = getMinValueIndexes(bigDecimals, minValueList, minIndexList); // 获取最小值索引\nSystem.out.println(indexes); // 输出:[1, 2]\n

Java 获取列表中所有最小值的索引 (不使用 list.get 方法)

原文地址: https://www.cveoy.top/t/topic/qaGD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录