数组与for循环

5.2数组

  除了前边说的字符串,数字,布尔这三个基础的数据类型外,其实还有一个基础数据类型,那就是数组,数组就是一组类型相同的数据组合在一起。

  比如这样一堆数字:1,3,5,7,9,11 我们用数组来存储这些数字

// 声明一个 int 数组的变量,数组大小为6
int[] numbers = new int[6];

numbers[0] = 1;
numbers[1] = 3;
numbers[2] = 5;
numbers[3] = 7;
numbers[4] = 9;
numbers[5] = 11;

同样如果想获取数组的某一个数据,也是用下标,语法如下

// 得到数组中第三个数字
int num = numbers[2];
System.out.println(num);

 

长度

  数组对象都有一个值length,可以得到该数组的长度

public static void main(String[] args) {
  int[] numbers = new int[8];

  int size = numbers.length;
  System.out.println(size);

}

数组初始化后,并没有储存实际的值,int类型的数据默认值是0,所以如果没有完成数组的赋值,那么int数组的每一个值都是0;String类型的数据默认值是null,所以没有赋值时,String数组的每一个值都是null.

我们来模拟一下excel表格,这个表格存放了学生成绩,第一列存放的是学生名称,第二列存放的是学生成绩

需要getScores,getNames和print方法,打印格式为 姓名:成绩,比如 小Z:40

public class Test{

  public static void main(String[] args) {
    String[] names = getNames();
    int[] scores = getScores();
    print(names, scores, 0, names.length);
  }

  // print 方法
public static void print(String[] names, int[] scores, int begin, int total){
  if (begin >= total) {
    return;
  }

  System.out.println(names[begin] + ":" + scores[begin]);
  // 递增begin
  begin++;
  print(names,scores,begin, total);
}
  

  public static int[] getScores() {

    int[] scores = new int[5];

    scores[0] = 88;
    scores[1] = 90;
    scores[2] = 70;
    scores[3] = 56;
    scores[4] = 40;

    return scores;
  }

  public static String[] getNames() {

    String[] names = new String[5];

    names[0] = "小D";
    names[1] = "小E";
    names[2] = "小F";
    names[3] = "小X";
    names[4] = "小Z";

    return names;
  }

}

运行结果为:

小D:88
小E:90
小F:70
小X:56
小Z:40


 

5.2 for循环

  如果我们想遍历数组中的每一项数据就需要用到循环的知识

  大多数情况下,我们操作表格数据都有两个需求

  • 录入数据到计算机中
  • 遍历分析数据

  首先录入数据

  

String[] tables = new String[3];

tables[0] = "张三";
tables[1] = "李四";
tables[2] = "王五";

 

  解决完存储之后,我们就需要遍历出数组中的每一条记录

for(int i = 0; i< tables.length;i++){
  String name = tables[i];
  System.out.println(name);
}

实际上数组的数据初始化,还有一个简写的方式,如果数据已经很确定的情况下,

那我们可以使用如下的语法

public static void main(String[] args) {
  String[] tables = new String[]{"张三","李四", "王五"};

  for(int i = 0; i < tables.length; i++){
    String name = tables[i];
    System.out.println(i+":"+name);
  }

}

如果是数字数组的话,那就是

new int[]{39,40,89};

浮点数据就是

new double[]{39,40,89};

还有一种for循环代码如下

for (String name : tables) {

}

这种称为增强for循环,等同于

for(int i = 0; i < tables.length; i++){
  String name = tables[i];
}

增强for循环是JDK1.5以后出来的一个高级for循环,专门用来遍历数组和集合的。它的内部原理其实是个Iterator迭代器,所以在遍历的过程中,不能对集合中的元素进行增删操作。

格式:

for(元素的数据类型 变量 : Collection集合or数组){
}

它用于遍历 Collection 和 数组。通常只进行遍历元素,不要在遍历的过程中对集合元素进行增删操作。

练习一:遍历数组

int[] arr = new int[]{11,22,33};
for (int n : arr) {
    //变量n代表被遍历到的数组元素</span>
    System.out.println(n);
}

练习二:遍历集合

Collection<String> coll = new ArrayList<String>();
coll.add("a1");
coll.add("a2");
coll.add("a3");
coll.add("a4");
for(String str : coll){
    //变量Str代表被遍历到的集合元素</span>
    System.out.println(str);
}

注意:新for循环必须有被遍历的目标。目标只能是Collection或者是数组。

建议:遍历数组时,如果仅为遍历,可以使用增强for如果要对数组的元素进行 操作


 

嵌套循环

  例:输出9*9乘法表

 

 代码:

public static void main(String[] args) {

  for (int i = 1; i <= 9; i++) {
    String line = "";
    for (int j = 1; j <= i; j++) {
      // 拼接line字符串,打印出 类似 1*2=2 2*2=4 这样的效果
      line = line + j + "*" + i + "=" + j * i + " ";
    }
    System.out.println(line);
  }

}

再回到学生成绩表,用for循环实现遍历

 1 public class Test6{
 2 
 3   public static void main(String[] args) {
 4     String[] names = getNames();
 5     int[] scores = getScores();
 6     print(names, scores);
 7   }
 8 
 9   public static void print(String[] names, int[] scores) {
10     for (int i = 0; i < names.length; i++) {
11 
12       System.out.println(names[i] + ":" + scores[i]);
13     }
14   }
15 
16 
17   public static int[] getScores() {
18 
19     int[] scores = new int[5];
20 
21     scores[0] = 88;
22     scores[1] = 90;
23     scores[2] = 70;
24     scores[3] = 56;
25     scores[4] = 40;
26 
27     return scores;
28   }
29 
30   public static String[] getNames() {
31 
32     String[] names = new String[5];
33 
34     names[0] = "小D";
35     names[1] = "小E";
36     names[2] = "小F";
37     names[3] = "小X";
38     names[4] = "小Z";
39 
40     return names;
41   }
42 
43 }

 

posted @ 2021-08-16 15:42  阿纳先森  阅读(523)  评论(0)    收藏  举报