[英]For-each and assigning to a 2d array Java
I'm using a for-each loop to move through a 2d array of chars in a basic java program. 我正在使用for-each循环在基本java程序中移动2d字符数组。 It works, except for a small part at the end.
它起作用,除了最后的一小部分。 Here is my class:
这是我的班级:
static String letters = "abcdefghijklmnopqrstuvwxyz";
public static boolean checkIsNumeric(String s) {
boolean haveBeenNonDigitCharacters = false;
char[] chars = s.toCharArray();
for(char c : chars)
if(!Character.isDigit(c)) {
haveBeenNonDigitCharacters = true;
break;
}
if(haveBeenNonDigitCharacters)
return false;
else
return true;
}
public static char[][] createArray() {
String height;
String width;
Scanner s = new Scanner(System.in);
do {
System.out.println("How tall do you want the array to be?");
height = s.next();
} while(!checkIsNumeric(height));
do {
System.out.println("How wide do you want the array to be?");
width = s.next();
} while(!checkIsNumeric(width));
int intHeight = Integer.parseInt(height);
int intWidth = Integer.parseInt(width);
char[][] chars = new char[intWidth][intHeight];
for(char c[] : chars) {
for(char d : c)
d = ' ';
}
return chars;
}
public static char[][] fillArray(char[][] a) {
int counter = 0;
for(char c[] : a) {
for(char d : c) {
if(counter >= 26)
counter = 0;
d = letters.charAt(counter);
counter++;
}
}
return a;
}
public static void main(String[] args) {
char[][] chars = createArray();
chars = fillArray(chars);
for(char c[] : chars) {
for(char d : c) {
System.out.print(d);
if(d == ' ')
System.out.print("a");
}
System.out.println("");
}
}
Basically, what I want to do, is take any 2d array with user-specified dimensions, and fill it with the letters of the alphabet over and over until it is full. 基本上,我想做的是采用任何具有用户指定尺寸的二维数组,并用字母表的字母一遍又一遍地填充它,直到它满了。 The
checkIsNumeric
method just checks to see if a String
is numeric, the createArray
method creates the 2d array with the user-specified dimensions, and the fillArray
method fills the array with letters from the string. checkIsNumeric
方法只检查String
是否为数字, createArray
方法使用用户指定的维度创建2d数组, fillArray
方法使用字符串中的字母填充数组。 But when I loop through the array at the end of main and print each character, nothing gets printed. 但是当我在main的末尾遍历数组并打印每个字符时,没有任何内容被打印出来。 Why?
为什么?
Change fillArray
to: 将
fillArray
更改为:
public static char[][] fillArray(char[][] a) {
int counter = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if(counter >= 26)
counter = 0;
a[i][j] = letters.charAt(counter);
counter++;
}
}
return a;
}
So that the actual array a
is being modified and not the copies created by the for-each loop. 这样就可以修改实际的数组
a
而不是for-each循环创建的副本。
Which will give the output (which I assume is correct): 这将给出输出(我假设是正确的):
How tall do you want the array to be?
5
How wide do you want the array to be?
4
abcde
fghij
klmno
pqrst
Same goes for createArray
if you want each char
to be initialised to ' '
. 如果您希望将每个
char
初始化为' '
,则createArray
如此。 In main
, if you cast d
as an int
and print it you'll see that each char
is set to it's initial value of 0. 在
main
,如果将d
为int
并打印它,您将看到每个char
都设置为它的初始值0。
You are assigning a char ( d ) the value ' ' , then the scope of d goes away. 您正在为char( d )赋值'' ,然后d的范围消失。 You are not assigning the array's value anything.
您没有分配任何数组的值。
In general: the problem is, that you can't assign new values to the variable defined in a for-each loop, and expect that the underlying data structure that is being iterated over gets modified. 通常:问题是,您不能将新值分配给for-each循环中定义的变量,并期望被迭代的基础数据结构被修改。 For instance this code has no effect on the array:
例如,此代码对数组没有影响:
int[] array = {1, 2, 3};
for (int e : array)
e = 0; // array doesn't change!
For actually modifying the array, you need to do this: 要实际修改数组,您需要这样做:
for (int i = 0, n = array.length; i < n; i++)
array[i] = 0; // now all elements in array are zero
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.