字符串遍历
public class ListFor { public static void main(String[] args) { List<String> list = Collections.synchronizedList(new ArrayList<String>());//返回由指定列表同步的线程安全的列表 ArrayList<String> array = new ArrayList<String>(); array.add("百事"); array.add("可乐"); array.add("芬达"); for(int i=0;i<array.size();i++){ String s = array.get(i); System.out.println(s); } } }
遍历对象,获取对象中的某个值
//查找对象中的 名字是否为小米 class test{ public static void main(String[] args) { ArrayList<Student> arrayList = new ArrayList<Student>(); Student s1 = new Student("小林", 30); Student s2 = new Student("小黑",20); Student s3 = new Student("小米",20); arrayList.add(s1); arrayList.add(s2); arrayList.add(s3); for(int i=0;i<arrayList.size();i++){ if(arrayList.get(i).getName().equals("小米")) { System.out.println(arrayList.get(i)); } } } }
遍历对象,获取对象中的某个值
class test01{ public static void main(String[] args) { ArrayList<Student> arrayList = new ArrayList<Student>(); Student s1 = new Student("小林", 30); Student s2 = new Student("小黑",20); Student s3 = new Student("小米",20); arrayList.add(s1); arrayList.add(s2); arrayList.add(s3); for(int i=0;i<arrayList.size();i++){ { Student s = arrayList.get(i); System.out.println(s.getName()+","+s.getAge()); } } } }