Java比较arraylist中数组的元素

我正在尝试将输入与数组列表中的值进行比较。

public compare(number)

前任; 我有一个数组列表:

[100,1102,14051,1024, / 142,1450,15121,1482,/ 141,1912,14924,1001] // the / represents each entry

数组的每个索引都代表我程序中的一个唯一属性。例如索引 0 表示user id,索引 1 表示room number等。如果我这样做arraylist.get(2),它会返回第二个数组(142,1450,15121,1482)

我正在尝试与number每个数组中的第二个元素进行比较。所以说我运行这个compare(1102),我希望它遍历每个数组中的每个 [1],如果该索引处有匹配项,则返回 true。

所以我希望它比较“数字”(1102)和每个第一个索引元素(1102,1450,1912),因为 1102 在数组列表中,所以返回 true

我一直在四处寻找,但找不到如何实现这一点,或者以正确的方式提出问题


繁华开满天机
浏览 169回答 3
3回答

慕哥9229398

Stream API 可以做到这一点。public class MyCompare&nbsp;{&nbsp; &nbsp; private static Optional<Integer> compare(Integer valueToCompare)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Optional<Integer[]> matchingArray = listToCompare.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(eachArray -> eachArray[1].equals(valueToCompare))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst();&nbsp; &nbsp; &nbsp; &nbsp; if(matchingArray.isPresent())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < listToCompare.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(listToCompare.get(i).equals(matchingArray.get()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Optional.of(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return Optional.empty();&nbsp; &nbsp; }&nbsp; &nbsp; private static List<Integer[]> listToCompare = new ArrayList<>();&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; listToCompare.add(new Integer[] { 100, 1102, 14051, 1024 });&nbsp; &nbsp; &nbsp; &nbsp; listToCompare.add(new Integer[] { 142, 1450, 15121, 1482 });&nbsp; &nbsp; &nbsp; &nbsp; listToCompare.add(new Integer[] { 141, 1912, 14924, 1001 });&nbsp; &nbsp; &nbsp; &nbsp; Optional<Integer> listIndex = compare(1102);&nbsp; &nbsp; &nbsp; &nbsp; if(listIndex.isPresent())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Match found in list index " + listIndex.get());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("No match found");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在列表索引 0 中找到匹配项

宝慕林4294392

遍历您的列表并每隔一个元素进行比较:public static boolean compare (int number){&nbsp; &nbsp; for( int i = 0; i<arraylist.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(number == arraylist.get(i)[1]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; return false;&nbsp;}

一只甜甜圈

直接的方法是使用增强的 for 循环:for (int[] items : list) {&nbsp; &nbsp; // do your checking in here}更高级但更优雅的方法是使用流:list.stream()&nbsp; &nbsp; &nbsp; &nbsp;// converts the list into a Stream.&nbsp; &nbsp; .flatMap(...)&nbsp; &nbsp;// can map each array in the list to its nth element.&nbsp; &nbsp; .anyMatch(...); // returns true if any value in the stream matches the Predicate passed.流 API:https ://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java