我有一个数组的ArrayList,我想改变5的值
List<int[]> list = new ArrayList<int[]>();
int[] arr1 = {2,4,6};
int[] arr2 = {1,3,5};
list.add(arr1);
list.add(arr2);
for (int[] ss : list)
{
for(int sd : ss)
{
//System.out.println(sd);
if(sd == 5)
{
System.out.println("Yes");
//change 5 to 12
//list.set(list.indexOf(5), 12);
}
}
System.out.println(Arrays.toString(ss));
}
[2,4,6] [1,3,5]
我想将5改为12
答案 0 :(得分:2)
将您的代码更改为:
public static void main(String args[]) {
List<int[]> list = new ArrayList<int[]>();
int[] arr1 = { 2, 4, 6 };
int[] arr2 = { 1, 3, 5 };
list.add(arr1);
list.add(arr2);
for (int[] ss : list) {
for (int i = 0; i < ss.length; i++) {
// System.out.println(sd);
if (ss[i] == 5) { // if current value is 5
System.out.println("Yes");
ss[i] = 12; // set it to 12
}
}
System.out.println(Arrays.toString(ss));
}
}