欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java自学-集合框架 ArrayList常用方法

程序员文章站 2022-07-04 22:32:44
ArrayList常用方法 步骤 1 : 增加 add 有两种用法: 第一种是直接add对象,把对象加在最后面 heros.add(new Hero("hero " + i)); 第二种是在指定位置加对象 heros.add(3, specialHero); package collection; ......

arraylist常用方法

步骤 1 : 增加

add 有两种用法:
第一种是直接add对象,把对象加在最后面

heros.add(new hero("hero " + i));

第二种是在指定位置加对象

heros.add(3, specialhero);

Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 把5个对象加入到arraylist中
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
        system.out.println(heros);
 
        // 在指定位置增加对象
        hero specialhero = new hero("special hero");
        heros.add(3, specialhero);
 
        system.out.println(heros.tostring());
 
    }
 
}
package charactor;
  
public class hero {
    public string name;
    public float hp;
  
    public int damage;
  
    public hero() {
  
    }
  
    // 增加一个初始化name的构造方法
    public hero(string name) {
  
        this.name = name;
    }
  
    // 重写tostring方法
    public string tostring() {
        return name;
    }
  
}

步骤 2 : 判断是否存在

通过方法contains 判断一个对象是否在容器中
判断标准: 是否是同一个对象,而不是name是否相同
Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 初始化5个对象
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
        hero specialhero = new hero("special hero");
        heros.add(specialhero);
 
        system.out.println(heros);
        // 判断一个对象是否在容器中
        // 判断标准: 是否是同一个对象,而不是name是否相同
        system.out.print("虽然一个新的对象名字也叫 hero 1,但是contains的返回是:");
        system.out.println(heros.contains(new hero("hero 1")));
        system.out.print("而对specialhero的判断,contains的返回是:");
        system.out.println(heros.contains(specialhero));
    }
 
}

步骤 3 : 获取指定位置的对象

通过get获取指定位置的对象,如果输入的下标越界,一样会报错 Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 初始化5个对象
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
        hero specialhero = new hero("special hero");
        heros.add(specialhero);
         
        //获取指定位置的对象
        system.out.println(heros.get(5));
        //如果超出了范围,依然会报错
        system.out.println(heros.get(6));
 
    }
 
}

步骤 4 : 获取对象所处的位置

indexof用于判断一个对象在arraylist中所处的位置
与contains一样,判断标准是对象是否相同,而非对象的name值是否相等
Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 初始化5个对象
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
        hero specialhero = new hero("special hero");
        heros.add(specialhero);
 
        system.out.println(heros);
        system.out.println("specialhero所处的位置:"+heros.indexof(specialhero));
        system.out.println("新的英雄,但是名字是\"hero 1\"所处的位置:"+heros.indexof(new hero("hero 1")));
 
    }
}

步骤 5 : 删除

remove用于把对象从arraylist中删除

remove可以根据下标删除arraylist的元素

heros.remove(2);

也可以根据对象删除

heros.remove(specialhero);

Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 初始化5个对象
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
        hero specialhero = new hero("special hero");
        heros.add(specialhero);
         
        system.out.println(heros);
        heros.remove(2);
        system.out.println("删除下标是2的对象");
        system.out.println(heros);
        system.out.println("删除special hero");
        heros.remove(specialhero);
        system.out.println(heros);
         
    }
}

步骤 6 : 替换

set用于替换指定位置的元素
Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 初始化5个对象
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
        hero specialhero = new hero("special hero");
        heros.add(specialhero);
         
        system.out.println(heros);
        system.out.println("把下标是5的元素,替换为\"hero 5\"");
        heros.set(5, new hero("hero 5"));
        system.out.println(heros);
    }
}

步骤 7 : 获取大小

size 用于获取arraylist的大小
Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 初始化5个对象
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
        hero specialhero = new hero("special hero");
        heros.add(specialhero);
        system.out.println(heros);
        system.out.println("获取arraylist的大小:");
        system.out.println(heros.size());
    }
}

步骤 8 : 转换为数组

toarray可以把一个arraylist对象转换为数组。
需要注意的是,如果要转换为一个hero数组,那么需要传递一个hero数组类型的对象给toarray(),这样toarray方法才知道,你希望转换为哪种类型的数组,否则只能转换为object数组

Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 初始化5个对象
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
        hero specialhero = new hero("special hero");
        heros.add(specialhero);
        system.out.println(heros);
        hero hs[] = (hero[])heros.toarray(new hero[]{});
        system.out.println("数组:" +hs);
 
    }
}

步骤 9 : 把另一个容器所有对象都加进来

addall 把另一个容器所有对象都加进来
Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 初始化5个对象
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
 
        system.out.println("arraylist heros:\t" + heros);
          
        //把另一个容器里所有的元素,都加入到该容器里来
        arraylist anotherheros = new arraylist();
        anotherheros.add(new hero("hero a"));
        anotherheros.add(new hero("hero b"));
        anotherheros.add(new hero("hero c"));
        system.out.println("anotherheros heros:\t" + anotherheros);
        heros.addall(anotherheros);
        system.out.println("把另一个arraylist的元素都加入到当前arraylist:");
        system.out.println("arraylist heros:\t" + heros);
         
    }
}

步骤 10 : 清空

clear 清空一个arraylist
Java自学-集合框架 ArrayList常用方法

package collection;
 
import java.util.arraylist;
 
import charactor.hero;
 
public class testcollection {
    public static void main(string[] args) {
        arraylist heros = new arraylist();
 
        // 初始化5个对象
        for (int i = 0; i < 5; i++) {
            heros.add(new hero("hero " + i));
        }
 
        system.out.println("arraylist heros:\t" + heros);
        system.out.println("使用clear清空");
        heros.clear();
        system.out.println("arraylist heros:\t" + heros);
          
    }
}