package com.it.tt5; import java.security.PublicKey; import java.util.ArrayList; public class Student { // public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>(); //add element arrayList.add("hello"); arrayList.add("world"); arrayList.add("java"); //delect direct element 删除置顶元素,返回是否删除成功 // public boolean remove(Object o); // System.out.println(arrayList.remove("hello")); // System.out.print(arrayList.remove(1));删除置顶索引的元素,并且返回被删除的元素 // console is world[hello, java] // 修改 // System.out.print(arrayList.set(1, "mimimi")); //world[hello, mimimi, java] 返回了被修改的元素并且修改、 // 搜索 // System.out.print(arrayList.get(1)); // System.out.print(arrayList.get(2)); // System.out.print(arrayList.get(3)); 会发生越界 //input console System.out.print(arrayList); } }
如图
studen学生信息添加的例子
stdentexment; package com.it.tt5; import java.util.ArrayList; import java.util.Scanner; import java.util.jar.Attributes.Name; public class studentexment { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Studtentest> arrayList = new ArrayList< >(); Scanner scanner = new Scanner(System.in); System.out.print("name:"); String naemString=scanner.nextLine(); System.out.println("age:"); String naemString2=scanner.nextLine(); Studtentest s = new Studtentest(); // 错误: Name a = new Name(null); // Age b = new Age(null); //创建学生对象 s.setName(naemString); s.setAge(naemString2); arrayList.add(s); //添加对象到集合中 for(int i=0;i<arrayList.size();i++) { Studtentest s5 = arrayList.get(i); System.out.print(s.getName()+","+s.getAge()); } System.out.print(s); } }
Studentest class package com.it.tt5; //学生累 public class Studtentest { private String name; private String age; public Studtentest(){ } public Studtentest (String name,String age) { this.name=name; this.age=age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
systemin:
name:12
age:12
返回:12,12com.it.tt5.Studtentest@13a57a3b
可以得出 返回了数组和地址