-
创建一个 char 类型的 26 个元素的数组,分别 放置’A’-‘Z’。使用 for 循环访问所有元素并打印出来。
-
提示:char 类型数据运算 ‘A’+2 -> ‘C’
-
思路分析
-
定义一个 数组 char[] chars = new char[26]
-
因为 ‘A’ + 1 = ‘B’ 类推,所以使用for来赋值
-
使用for循环访问所有元素
char[] chars = new char[26];
for( int i = 0; i < chars.length; i++)
{//循环26次
//chars 是 char[]
//chars[i] 是 char
chars[i] = (char)('A' + i); //'A' + i 是int , 需要强制转换
}
//循环输出
System.out.println("===chars数组===");
for( int i = 0; i < chars.length; i++) {//循环26次
System.out.print(chars[i] + " ");
}
-
请求出一个数组 int[]的最大值 {4,-1,9, 10,23},并得到对应的下标。
思路分析:
定义一个int数组 int[] arr = {4,-1,9, 10,23};
假定 max = arr[0] 是最大值 , maxIndex=0;
从下标 1 开始遍历arr, 如果max < 当前元素,说明max 不是真正的最大值, 我们就
max=当前元素; maxIndex=当前元素下标
当我们遍历这个数组arr后 , max就是真正的最大值,maxIndex最大值对应的下标
int[] arr = {4,-1,9,10,23};
int max = arr[0];//假定第一个元素就是最大值
int maxIndex = 0; //
for(int i = 1; i < arr.length; i++) {//从下标 1 开始遍历arr
if(max < arr[i]) {//如果max < 当前元素
max = arr[i]; //把 max 设置成 当前元素
maxIndex = i;
}
}
//当我们遍历这个数组arr后 , max就是真正的最大值,maxIndex最大值下标
System.out.println("max=" + max + " " + "maxIndex=" + maxIndex);
-
数组遍历输出:一次性把数组中所有的元素输出
public class TestArray02 {
public static void main(String[] args) {
String[] arr=new String[5];
arr[0]="刘备";
arr[1]="孙权";
arr[2]="曹操";
arr[3]="关羽";
arr[4]="宋江";
//数组中的元素一个一个输出:
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
System.out.println("================================");
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
-
数组中存放的数据类型是自定义的类型(引用数据类型)
-
有一个学生类,学生类有一些属性,创建5个学生存放到数组中
public class Student {
private String name;//名字
private String stuID;//学号
private String hobby;//爱好
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuID() {
return stuID;
}
public void setStuID(String stuID) {
this.stuID = stuID;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Student(String name, String stuID, String hobby) {
this.name = name;
this.stuID = stuID;
this.hobby = hobby;
}
public Student() {
}
//toString()方法
// @Override
// public String toString() {
// return "Student{" +
// "名字='" + name + '\'' +
// ", 学号='" + stuID + '\'' +
// ", 爱好='" + hobby + '\'' +
// '}';
// }
}
public class TestStudent {
public static void main(String[] args) {
Student s1=new Student("曹操","0x115","打仗");
Student s2=new Student("曹植","0x117","写诗");
Student s3=new Student("李白","0x118","喝酒");
Student s4=new Student("刘备","0x111","关羽");
Student s5=new Student("孙权","0x110","篮球");
//数据类型【】 数组名={值1,值2,值3};
Student[] stus={s1,s2,s3,s4,s5};
//遍历
//for循环,循环的都是下标
//最后一位的下标 是 length-1
System.out.println("----------------------");
System.out.println("|名字|\t|学号|\t|爱好|");
System.out.println("----------------------");
for(int i=0;i<stus.length;i++){
Student stu=stus[i];
System.out.println("|"+stu.getName()+"|\t"+"|"+stu.getStuID()+"|\t"+"|"+stu.getHobby()+"|");
System.out.println("----------------------");
}
}
}