[英]How do I find the number of occurences of repeating elements in array in Java EE?
[英]How can I print number of occurences of an array from an array list in Java?
我有一個包含一些數組的數組列表,我想打印出數組列表,如果數組列表中的數組重復,則只打印一次,旁邊出現多次。 (假設所有數組都是從小到大排序的)
這是我當前的代碼:
public static void main(String[] args) {
int[] a1 = {1,2,3,4,5};
int[] a2 = {1,2,3,4,5};
int[] a3 = {1,2,3,4,5};
int[] a4 = {2,5,7,8,9};
int[] a5 = {2,5,7,8,9};
int[] a6 = {3,4,6,8,9};
ArrayList<int[]> list = new ArrayList<>();
list.add(a1);
list.add(a2);
list.add(a3);
list.add(a4);
list.add(a5);
list.add(a6);
for (int i = 0; i < list.size(); i++) {
System.out.println(Arrays.toString(list.get(i)));
}
}
這是我想要的輸出:
1,2,3,4,5 (3)
2,5,7,8,9 (2)
3,4,6,8,9
(如果它是唯一的,則不需要出現次數)
非常感謝您的幫助!
使用List<Integer>
而不是int[]
,因為 List 的equals()
(和hashCode()
)方法可以起到很好的效果。
假設:
List<List<Integer>> list = new ArrayList<>();
list.add(List.of(1,2,3,4,5));
// etc
然后是單行:
list.stream().distinct().forEach(System.out::println);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.