ArrayList类
引入对象数组
package day16.demo01;
public class person {
private String name;
private int age;
public person() {
super();
}
public person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package day16.demo01;
public class demo01Array {
public static void main(String[] args) {
person [] array =new person[3];
person one =new person("第一人",18);
person two=new person("第二人",19);
person theer =new person("第三人",20);
array[0]=one;
array[1]=two;
array[2]=theer;
System.out.println(array);
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[2].getName());
}
}
ArrayListe
package day16.demo02;
import java.util.ArrayList;
public class demo02A {
public static void main(String[] args) {
ArrayList<String> list =new ArrayList<String>();
System.out.println(list);
System.out.println("===============");
list.add("第一人");
System.out.println(list);
System.out.println("===============");
list.add("第二人");
list.add("第三人");
System.out.println(list);
//错误写法:list.add(100);
}
}
常用方法
package day16.demo02;
import java.util.ArrayList;
public class demo02B {
public static void main(String[]args) {
ArrayList<String> list =new ArrayList<String>();
boolean success =list.add("第一人");
System.out.println(""+success);
System.out.println(list);
list.add("第二人");
list.add("第三人");
list.add("第四人");
list.add("第五人");
System.out.println(list);
//下标从0开始
String name =list.get(3);
System.out.println("索引3号位置"+name);
int size =list.size();
System.out.println(size);
String whoRemoved =list.remove(3);
System.out.println("移除"+whoRemoved);
System.out.println(list);
int size2 =list.size();
System.out.println(size2);
for(int i=0;i<list.size();i++) {
System.out.println(size2);
}
}
}
如何存储基本数据类型
package day16.demo02;
import java.util.ArrayList;
/*
* byte Byte
* short Short
* int Integer[特殊]
* long Long
* float Float
* double Double
* char Character【特殊】
* boolean Boolean
* */
public class demo02C {
public static void main(String[] args) {
ArrayList<String> listA =new ArrayList<String>();
//错误写法:
//ArrayList<int> listA =new ArrayList<int>();
ArrayList<Integer> listB =new ArrayList<>();
listB.add(100);
listB.add(200);
System.out.println(listB);
int num=listB.get(1);
System.out.println(num);
ArrayList<Double> listc =new ArrayList<>();
listc.add(100.0);
System.out.println(listc);
}
}
2020080605012