使用与整型数组一个ArrayList包含 [英] Using contains on an ArrayList with integer arrays

查看:94
本文介绍了使用与整型数组一个ArrayList包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ArrayList< INT []方式> ,我添加阵列它

I have an ArrayList<int[]>, and I add an array to it.

ArrayList<int[]> j = new ArrayList<int[]>();
int[] w = {1,2};
j.add(w);

假设我想知道,如果Ĵ包含有 它不使用{1,2}数组是W ,因为我会从另一个类调用它。所以,我创建新阵列{1,2} 在里面...

Suppose I want to know if j contains an array that has {1,2} in it without using w, since I will be calling it from another class. So, I create a new array with {1,2} in it...

int[] t = {1,2};
return j.contains(t);

...但这即使返回false是W 添加到列表中,是W 包含相同的阵列 T

...but this would return false even though w was added to the list, and w contains the exact same array as t.

有没有办法使用的方法包含这样我可以只检查,看看是否的ArrayList 元素中的一个具有数组值 { 1,2}

Is there a way to use contains such that I can just check to see if one of the elements of the ArrayList has the array value {1,2}?

推荐答案

阵列只能与满足Arrays.equals比较()。

Arrays can only be compared with Arrays.equals().

您可能想的ArrayLists的ArrayList

You probably want an ArrayList of ArrayLists.

ArrayList<ArrayList<Integer>> j = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> w = new ArrayList<Integer>();
w.add(1); w.add(2);
j.add(w);
ArrayList<Integer> t = new ArrayList<Integer>();
t.add(1); t.add(2);
return j.contains(t); // should return true.

这篇关于使用与整型数组一个ArrayList包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆