标题: 遍历 [打印本页] 作者: 超级无敌法医佬 时间: 2019-9-20 22:40 标题: 遍历 1、数组的遍历
public class ShuZuBianLi{
public static void main(String[] args){
int [] arr={20,19,9,22};
for (int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
2、字符串的遍历
public class ZiFuChuanBianLi{
public static void main(String[] args){
String s =”itheima”;
for (int i=0;i<s.length();i++){
System.out.println(s.charAt(i));
}
}
}
3、集合的遍历
//教师类
public class Teacher{
private String name;
public Teacher(){
}
public Teacher(String name){
this.name=name;}
public String getName(){
return name;}
public void setName(String name){
this.name=name;}
}
//教师测试类
import java.util.ArrayList;
public class TeacherTest{
public static void main(String[] args){
Teacher teacher1=new Teacher();
Teacher teacher2=new Teacher();
Teacher teacher3=new Teacher();
ArrayList<Teacher>array=new ArrayList<Teacher>();
teacher1.setName(“葛老板”);
teacher2.setName(“静静”);
teacher3.setName(“刚哥”);
array.add(teacher1);
array.add(teacher2);
array.add(teacher3);
for (int i=0;i<array.size();i++){
Teacher t=array.get(i);
System.out.println(t.getName());
}
}
}