这个 for 循环如何知道这个数组数据结构从一开始就有多长时间?
How does this for-loop know how long this array datastructure will be from the start?
我试图弄清楚数组如何知道 for 循环中 System.out.println 的第一个 运行 总共有 15 个索引,以及它确切知道的位置.
好像for循环还在执行的时候就知道索引的总数了,还以为数组一旦创建就不能更改了。数组在构造时是否处于临时状态,或者答案比我做的要简单得多?
我正在猜测可能的答案,我不确定。请帮忙!提前谢谢你。
我使用这段代码的唯一目的是评估底部的结果输出与用于创建它的代码相比,并确定这个特定的 for 循环如何构造数组。
import java.util.Arrays;
public class DataStructureMain {
public static void main(String[] args) {
class DataStructure {
//Create an array
private final static int SIZE = 15;
private int[] anArrayOfInts = new int[SIZE];
public DataStructure() {
//Fill the array with ascending integer values
for (int i = 0; i < SIZE; i++) {
anArrayOfInts[i] = i;
//I put this next line in here to see how the code
//executes each run of the for-loop
System.out.println(Arrays.toString(anArrayOfInts));
}
}
}
//
//
DataStructure dS = new DataStructure();
}
}
产生这个:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
你设置
SIZE = 15;
然后你使用相同的变量来构造数组
private int[] anArrayOfInts = new int[SIZE];
。
创建后其大小是固定的。
然后你使用相同的变量来限制迭代次数
for (int i = 0; i < SIZE; i++)
或者你可以使用:
for (int i = 0; i < anArrayOfInts.length ; i++)
这会给你相同的结果。
您正在使用:
private final static int SIZE = 15;
然后在整个程序中使用此 SIZE 变量。所以编译器不会做任何事情来检查数组的大小,因为你每次都指定大小。
相反,您可以使用此代码来了解更多信息:
Scanner s= new Scanner(System.in); // to take input from user;
int n;
n = s.nextInt(); // takes integer input from user.
int arr[]=new int[n]; //array of size n,specified by user
for(int i=0;i<arr.length;i++) //in place of arr.length you can use n also
System.out.println(arr[i]);
arr.length 是一个 属性 所以它不会是 arr.length() (这是一个函数),它包含数组的长度。
更多信息,您可以访问这个link:
Array Length in Java
我试图弄清楚数组如何知道 for 循环中 System.out.println 的第一个 运行 总共有 15 个索引,以及它确切知道的位置.
好像for循环还在执行的时候就知道索引的总数了,还以为数组一旦创建就不能更改了。数组在构造时是否处于临时状态,或者答案比我做的要简单得多?
我正在猜测可能的答案,我不确定。请帮忙!提前谢谢你。
我使用这段代码的唯一目的是评估底部的结果输出与用于创建它的代码相比,并确定这个特定的 for 循环如何构造数组。
import java.util.Arrays;
public class DataStructureMain {
public static void main(String[] args) {
class DataStructure {
//Create an array
private final static int SIZE = 15;
private int[] anArrayOfInts = new int[SIZE];
public DataStructure() {
//Fill the array with ascending integer values
for (int i = 0; i < SIZE; i++) {
anArrayOfInts[i] = i;
//I put this next line in here to see how the code
//executes each run of the for-loop
System.out.println(Arrays.toString(anArrayOfInts));
}
}
}
//
//
DataStructure dS = new DataStructure();
}
}
产生这个:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
你设置
SIZE = 15;
然后你使用相同的变量来构造数组
private int[] anArrayOfInts = new int[SIZE];
。
创建后其大小是固定的。
然后你使用相同的变量来限制迭代次数
for (int i = 0; i < SIZE; i++)
或者你可以使用:
for (int i = 0; i < anArrayOfInts.length ; i++)
这会给你相同的结果。
您正在使用:
private final static int SIZE = 15;
然后在整个程序中使用此 SIZE 变量。所以编译器不会做任何事情来检查数组的大小,因为你每次都指定大小。
相反,您可以使用此代码来了解更多信息:
Scanner s= new Scanner(System.in); // to take input from user;
int n;
n = s.nextInt(); // takes integer input from user.
int arr[]=new int[n]; //array of size n,specified by user
for(int i=0;i<arr.length;i++) //in place of arr.length you can use n also
System.out.println(arr[i]);
arr.length 是一个 属性 所以它不会是 arr.length() (这是一个函数),它包含数组的长度。
更多信息,您可以访问这个link: Array Length in Java