泛型数组列表

时间:2022-02-04 19:46:50
构造,初始化,访问元素,添加元素,删除元素,修改元素
子类构造器
对象包装器(Integer)/数字与字符串之间的转换
 
 
package zzy;import java.util.*;import static java.lang.System.*;/** * * @author Zhu Zhengyan */public class shuzu2 {    public static void main(String[] args){    ArrayList<NIUBI> a = new ArrayList<>();//定义数组a      也可 NIUBI[] b = new NIUBI[3];  b[0] = new NIUBI(1,"Bkj");    a.ensureCapacity(2);       //确认容量,也可直接在上方()中写2    a.add(new NIUBI(5,"hdasd"));    a.add(new NIUBI(42,"nihaoa"));  //将元素添加到数组列表中    NIUBI m = a.get(0);   //获得数组列表的元素,等价于     NIUBI m = a[0];    m.show();    a.set(0,new NIUBI(2,"s"));  //等价于a[0] = ....    set只能替换数组中存在的元素内容,不能用于定义    NIUBI n = a.get(0);    n.show();    a.add(1,new NIUBI(8,"h"));   //插入一个新元素到1号位,原来的1号位及其之后的元素向后移动    NIUBI m_ = a.get(2);    m_.show();    NIUBI n_ = a.get(1);    n_.show();    NIUBI o_ = a.remove(1);     //删除一个元素,它之后的元素都向前移动    NIUBI n__ = a.get(1);    n__.show();        ArrayList<Integer> j = new ArrayList<>(5);    j.add(9);    int j0 = j.get(0);    //out.println(j0);    String s = "1";    int s_ = Integer.parseInt(s);    //将数字字符串转换为数字    //out.println(s_);    int x = 5;    String x_ = Integer.toString(x);  //将数字转换为字符串    //out.println(x_);    }}class NIUBI{    private int a ;    private String b ;    private double c;    public NIUBI(int a,String b){        this.a = a;        this.b = b;    }    public String getString(){        return (b);    }    public void show(){       out.println(a+","+b);    }}class niu extends NIUBI{    private char d;        public niu(int a, String b,char d){        super( a, b);        this.d = d;    }}