前言——容器的學習,剛開始建議先掌握一些基本概念。個人經驗覺得,初學時,必須先從模仿開始,掌握基本方法,然後在平常的使用當中進一步深究,只會使用,內部原理一竅不懂也是不行的,debug的時候會無從下手,這篇文章純粹的屬於應用篇,入門使用篇,非常適合剛學習容器。對了,文章的精華是每個方法的例子,簡單易懂真的推薦鴨
目錄
Hero類作爲測試類
package Charactor;
import java.io.Serializable;
public class Hero {
public String name ;
public float hp;
public float damage;
public Hero() {
}
public Hero(String n, float h, float d) {
name = n;
hp = h;
damage = d;
}
public float getHp() {
return hp;
}
//重寫Object方法是因爲:
//輸出容器中的對象時,調用的是toString()方法,重寫後可以輸出自定義內容。
public String toString(){
return name;
}
}
一.使用數組的侷限性
數組雖然常用,但是數組的侷限性很多,最明顯的就是使用數組之前必須事先定義好長度。定義得過大,沒用完會浪費空間,定義得過小,會不夠用,無法自動擴大長度。
package Test;
import Charactor.Hero;
public class TestArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Hero [] heros = new Hero[10];//創建一個存放英雄對象的數組,長度爲10
for(int i = 0 ;i < 10 ;i++) { //存放10個英雄
heros[i] = new Hero();
}
//打算存放第11個英雄,會報錯
heros[10] = new Hero();
}
}
運行時會拋出下標越界的異常:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Test.TestArray.main(TestArray.java:19)
二.因爲數組的侷限性,引入了容器
數組無法實現長度的動態增長,是引入容器的重要原因。這邊先介紹一個容器,也算是用得比較多的——ArrayList。
容器提供了很多強大的功能,這邊我給大家介紹容器最大的好處——容量可以動態增長。
package Test;
import java.util.ArrayList;
import Charactor.Hero;
public class TestArrayList {
public static void main(String[] args) {
// TODO Auto-generated method stub
//創建一個ArrayList的容器對象,大家會發現不用指定長度
ArrayList heros = new ArrayList();
heros.add(new Hero());
/*可以往容器裏面加入無限個對象,優點很明顯
heros.add(new Hero());
.....
.....
*/
//heros.size()用來返回容器的長度,也就是對象的個數
System.out.println(heros.size());
}
}
三.List的常用方法
1.增加一個對象——add
a add(Object)
b. add(int i,Object)插入在第i個位置(i從0開始)
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.addTest();
}
public void addTest() {
for(int i = 0 ;i < 5;i++) {
//方法1,直接把對象add在容器最後面
heros.add(new Hero("hero" + i, 1,1));
}
//輸出容器裏面的內容
System.out.println(heros);
//方法2,把對象add在某個下標(下標從0開始)
heros.add(2,new Hero("myHero" ,1 ,1));
//再次輸出容器裏面的內容
System.out.println(heros);
}
}
2.判斷某個對象是否在容器中------contains
contains(Object);
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.containsTest();
}
public void containsTest() {
for(int i = 0 ;i < 5;i++) {
heros.add(new Hero("hero" + i, 1,1));
}
Hero myHero = new Hero();
heros.add(2,myHero);
//雖然這個對象的名字也是hero1,但是沒有找到
System.out.println(heros.contains(new Hero("hero1",2,2)) );
//查詢容器中是否存在和myHero指向共同對象的引用,輸出的是True
System.out.println(heros.contains(myHero));
}
}
/*
運行結果:
false
true
*/
這邊要注意:容器中存儲的都是引用,都是指向對象的引用。判斷某個對象是否在容器中,其實就是判斷容器中是否存在某個引用和傳入的引用指向共同對象。不是查找名字相同的對象是否存在,這點要切記!!!
額外話題:那麼如何找到name爲"hero1"的元素呢?
思路:肯定是遍歷啦,直接for循環+get()方法(下面就會介紹了,這邊先貼個代碼)
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.isName();
}
public void isName() {
for(int i = 0 ;i < 5 ;i++) {
heros.add(new Hero("hero" + i ,1,1));
}
/*
轉化爲數組來遍歷也可以,不過麻煩了,多此一舉
Hero [] myHero= (Hero [])heros.toArray(new Hero[]{});
for(int i = 0 ;i < myHero.length ;i++) {
if(myHero[i].name.equals("hero1")) {
System.out.println("找到了");
break;
}
}
System.out.println(heros);
*/
for(int i = 0 ;i < heros.size();i++) {
Hero temp = (Hero )heros.get(i);
if(temp.name.equals("hero1")) {
System.out.println("找到了");
break;
}
}
}
}
3.獲取指定位置的對象-----get
get(int index), 注意index不能越界,不然會拋出異常
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.getTest();
}
public void getTest() {
for(int i = 0 ;i < 5;i++) {
heros.add(new Hero("hero" + i, 1,1));
}
Hero myHero = new Hero("myHero", 1,1);
heros.add(2,myHero);
//下標2,沒有越界
System.out.println(heros.get(2));
//下標8,越界了,會報錯
// System.out.println(heros.get(8));
}
}
/*
運行結果:
myHero
*/
4.獲取對象所處的位置-----indexOf
heros.indexOf(Object);
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.indexOfTest();
}
public void indexOfTest() {
for(int i = 0 ;i < 5;i++) {
heros.add(new Hero("hero" + i, 1,1));
}
Hero myHero = new Hero("myHero", 1,1);
heros.add(2,myHero);
//查找myHero這個對象,能查找到,輸出2
System.out.println(heros.indexOf(myHero));
//查找新創建的對象,沒能查找到,輸出-1
System.out.println(heros.indexOf(new Hero("hero1",1,1)));
}
}
/*
運行結果:
2
-1
*/
這和contains一樣,判斷的是容器中是否有某個引用和要查詢的引用指向同一對象,弄不懂的同學,要去學習一下引用的概念。
5.刪除容器中的對象
可以通過下標來刪除------remove(int index)
也可以通過對象來刪除------remove(Object);
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.removeTest();
}
public void removeTest() {
for(int i = 0 ;i < 5;i++) {
heros.add(new Hero("hero" + i, 1,1));
}
Hero myHero = new Hero("myHero", 1,1);
heros.add(2,myHero);
System.out.println(heros);
//移除容器中下標1對應的對象
heros.remove(1);
System.out.println(heros);
//移除容器中的myHero對象
heros.remove(myHero);
System.out.println(heros);
}
}
/*
運行結果:
[hero0, hero1, myHero, hero2, hero3, hero4]
[hero0, myHero, hero2, hero3, hero4]
[hero0, hero2, hero3, hero4]
*/
6.替換容器中某個位置的元素
set(int index ,Object)
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.setTest();
}
public void setTest() {
for(int i = 0 ;i < 5;i++) {
heros.add(new Hero("hero" + i, 1,1));
}
Hero myHero = new Hero("myHero", 1,1);
heros.add(2,myHero);
System.out.println(heros);
System.out.println("把下標是3的元素替換成\" myHero2\" ");
heros.set(3, new Hero("myHero2",1,1));
System.out.println(heros);
}
}
/*
運行結果:
[hero0, hero1, myHero, hero2, hero3, hero4]
把下標是3的元素替換成" myHero2"
[hero0, hero1, myHero, myHero2, hero3, hero4]
*/
7.獲取容器當前的長度,也就是當前存儲的對象的數量
size();
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.sizeTest();
}
public void sizeTest() {
for(int i = 0 ;i < 5;i++) {
heros.add(new Hero("hero" + i, 1,1));
}
Hero myHero = new Hero("myHero", 1,1);
heros.add(2,myHero);
System.out.println("當前長度爲:" + heros.size());
}
}
8.轉化爲某種類型的數組
toArray();
有兩種重載方法:
toArray():返回的是Object類型的數組,無參,不允許強制裝換, 比如(T [])toArray()是錯誤的
toArray(T[] t): 返回的是Object類型的數組,有參數,需要傳一個數組對象,一般用這個方法
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.toArrayTest();
}
public void toArrayTest() {
for(int i = 0 ;i < 5;i++) {
heros.add(new Hero("hero" + i, 1,1));
}
Hero myHero = new Hero("myHero", 1,1);
heros.add(2,myHero);
//這邊要注意,應該傳一個Hero類型的數組對象給toArray()
Hero [] temp = (Hero [])heros.toArray(new Hero [] {});
System.out.println(temp);
}
}
/*
運行結果:
[LCharactor.Hero;@7852e922
*/
9.把另一個容器的所有對象加進來
addAll(Colection c);
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.addAllTest();
}
public void addAllTest() {
for(int i = 0 ;i < 5;i++) {
heros.add(new Hero("hero" + i, 1,1));
}
ArrayList myHeros = new ArrayList();
myHeros.add(new Hero("hero5",2,2));
myHeros.add(new Hero("hero6",2,2));
heros.addAll(myHeros);
System.out.println(heros);
}
}
/*
運行結果:
[hero0, hero1, hero2, hero3, hero4, hero5, hero6]
*/
10.清空一個容器
clear();
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
ArrayList heros = new ArrayList();
public static void main(String []args) {
TestContainerMethod myTest = new TestContainerMethod();
myTest.clearTest();
}
public void clearTest() {
for(int i = 0 ;i < 5;i++) {
heros.add(new Hero("hero" + i, 1,1));
}
heros.clear();
System.out.println(heros);
}
}
/*
運行結果:
[]
*/
四.ArrayList與List的關係
ArrayList實現了List接口:
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess,Cloneable,java.io.Serializable{
}
上面所介紹的10種方法都是實現List中的方法得到的,我們有時也可以創建一個List的引用,令這個引用指向ArrayList的對象(多態):
package Test;
import java.util.*;
import Charactor.Hero;
public class TestContainerMethod {
public static void main(String []args) {
List myheros = new ArrayList();
myheros.add(new Hero("myHero",1,1));
System.out.println(myheros);
}
}