package heima;
import java.util.ArrayList;
//import javax.sound.sampled.Line;
import java.util.Scanner;
public class a1 {
public static void main(String[] args) {
ArrayList<String>array=new ArrayList<String>();
array.add("hello");
array.add("word");
array.add("java");
array.add("k");
array.add(1,"javase");
array.add(3,"javase");
System.out.println(array);
}
}
package heima;
import java.util.ArrayList;
//import javax.sound.sampled.Line;
import java.util.Scanner;
public class a1 {
public static void main(String[] args) {
ArrayList<String>array=new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
// System.out.println(array.remove("world"));
// System.out.println(array.remove("javaee"));
// System.out.println(array.remove(1));
// System.out.println(array.set(1,"修改"));
// System.out.println(array.get(0));
// System.out.println(array.size());
System.out.println(array);
}
}
遍历String
package heima;
import java.util.ArrayList;
//import javax.sound.sampled.Line;
import java.util.Scanner;
public class a1 {
public static void main(String[] args) {
ArrayList<String>array=new ArrayList<String>();
array.add("刘正风1");
array.add("刘正风2");
for(String k:array){
System.out.println(k);
}
}
}
遍历学生类
(1)
package heima;
public class Student {
private String name;
private int age;
public Student(){}
public Student(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
package heima;
import java.util.ArrayList;
//import javax.sound.sampled.Line;
import java.util.Scanner;
public class a1 {
public static void main(String[] args) {
ArrayList<Student>array=new ArrayList<Student>();
Student s1=new Student("为对方",23);
Student s2=new Student("Wdq",23);
Student s3=new Student("权威的",44);
array.add(s1);
array.add(s2);
array.add(s3);
for(int i=0;i<3;i++){
Student s=array.get(i);
System.out.println(s.getName()+","+s.getAge());
}
}
}
(2)
package heima;
public class Student {
private String name;
private int age;
public Student(){}
public Student(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
package heima;
import java.util.ArrayList;
//import javax.sound.sampled.Line;
import java.util.Scanner;
public class a1 {
public static void main(String[] args) {
ArrayList<Student>array=new ArrayList<Student>();
add(array);
add(array);
add(array);
for(int i=0;i< array.size();i++){
Student s=array.get(i);
System.out.println(s.getName()+","+s.getAge());
}
}
public static void add(ArrayList<Student>array){
Scanner k=new Scanner(System.in);
System.out.println("姓名");
String name=k.nextLine();
System.out.println("年龄");
int age=k.nextInt();
Student s=new Student();
s.setName(name);
s.setAge(age);
array.add(s);
}
}