1-D arrays: store, index, and traverse · 一维数组:存储、下标和遍历
Many values in one box
- An array holds many values of the same type in order.
- Until now one variable held one value. An array holds a whole list.
- For example, the test scores of a class can live in one
intarray.
一个盒子里装很多值
- 一个数组(array)按顺序保存很多个相同类型的值。
- 在此之前,一个变量只保存一个值。数组保存一整个列表。
- 例如,一个班的考试分数可以放在一个
int数组里。
Make an array with values
- If you know the values, list them in curly braces
{ }. - The type is the element type plus
[], likeint[]. - Java counts how many values you gave and makes the array that size.
用值来创建数组
- 如果你知道这些值,就把它们列在花括号
{ }里。 - 类型是元素类型再加
[],比如int[]。 - Java 会数你给了多少个值,并创建对应大小的数组。
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
System.out.println(scores[0]); // 90
System.out.println(scores[2]); // 70
}
}
Index positions
- Each value has an index (its position). Indexes start at 0.
- So the first value is
scores[0], the second isscores[1], and so on. - You can also change a value:
scores[1] = 85;.
下标位置
- 每个值都有一个下标(index,它的位置)。下标从 0 开始。
- 所以第一个值是
scores[0],第二个是scores[1],以此类推。 - 你也可以修改一个值:
scores[1] = 85;。
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
scores[1] = 85; // change the middle value
System.out.println(scores[1]); // 85
}
}
Make an empty array
- Use
new int[n]to make an array ofnvalues. - Every
intslot starts at 0 automatically. - Then you fill the slots one by one.
创建一个空数组
- 用
new int[n]来创建一个有n个值的数组。 - 每个
int位置都会自动从 0 开始。 - 然后你一个一个地填进去。
public class Main {
public static void main(String[] args) {
int[] nums = new int[3]; // {0, 0, 0}
nums[0] = 5;
nums[1] = 6;
System.out.println(nums[0] + " " + nums[1] + " " + nums[2]); // 5 6 0
}
}
.length
array.lengthtells you how many values the array has.- Note: no parentheses — it is
scores.length, notscores.length(). - The last valid index is always
length - 1.
.length
array.length告诉你数组里有多少个值。- 注意:没有圆括号 —— 是
scores.length,不是scores.length()。 - 最后一个有效下标永远是
length - 1。
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
System.out.println(scores.length); // 3
System.out.println(scores[scores.length - 1]); // 70 (last value)
}
}
Traverse with a for loop
- To visit every value, loop the index from
0up tolength - 1. - Use
i < array.lengthas the stop test. - Below we add up every value.
用 for 循环遍历
- 要访问每个值,就让下标从
0循环到length - 1。 - 用
i < array.length作为停止条件。 - 下面我们把每个值加起来。
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
int total = 0;
for (int i = 0; i < scores.length; i++) {
total = total + scores[i];
}
System.out.println(total); // 240
}
}
Enhanced for loop
- The enhanced for (or "for-each") visits each value directly.
- Read it as: "for each
int vinscores". - Use it when you only need the values, not the index.
增强 for 循环
- 增强 for(也叫 “for-each”)直接访问每一个值。
- 可以这样读:“对
scores里的每一个int v”。 - 当你只需要值、不需要下标时,就用它。
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
int total = 0;
for (int v : scores) {
total = total + v;
}
System.out.println(total); // 240
}
}
Common mistakes
a.lengthhas no(); valid indexes run0toa.length - 1.- Going out of range throws ArrayIndexOutOfBoundsException.
常见错误
a.length不带();合法下标是0到a.length - 1。- 越界会抛出 ArrayIndexOutOfBoundsException。
Now you try
- Some tasks print a result; others complete a method the Harness calls.
- Remember: indexes start at 0, and the last index is
length - 1. - Press Run to compile, then Check answer.
现在轮到你
- 有些任务打印一个结果;有些任务要补全一个由 Harness 调用的方法。
- 记住:下标从 0 开始,最后一个下标是
length - 1。 - 按运行来编译,然后按检查答案。
A 1-D array, indexed · 一维数组,有索引
An array stores values in order; reach one with a[i]. · 数组按顺序存值;用 a[i] 取一个。
The array nums is already filled. Use a loop to add up all its values and print the total on one line. For {4, 8, 15, 16, 23, 42} the output is 108. · 数组 nums 已经填好了。用一个循环把它所有的值加起来,并在一行里打印总和。对 {4, 8, 15, 16, 23, 42},输出是 108。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete sum(int[] a) so it returns the sum of all values in the array. An empty array should return 0. The checker calls it with several arrays. · 完成 sum(int[] a),让它返回数组里所有值的和。空数组应当返回 0。检查器会用多个数组来调用它。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete doubleAll(int[] a). It does not return anything. Instead it changes the array in place so every value becomes twice as big. {1, 2, 3} becomes {2, 4, 6}. · 完成 doubleAll(int[] a)。它不返回任何东西。它直接就地修改数组,让每个值变成原来的两倍。{1, 2, 3} 变成 {2, 4, 6}。
Click Run to see the output here. · 点击“运行”查看此处输出。