簡單的泛型使用類似下面code,第一段是簡單的int泛型,第二段是int[]的陣列。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void parameterInt() | |
{ | |
ArrayList<Integer> arrayList=new ArrayList<Integer>(); | |
for(int i=0;i<10;i++) | |
{ | |
arrayList.add(i); | |
} | |
for(int i:arrayList) | |
{ | |
System.out.print(i+","); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void parameterIntArray() | |
{ | |
ArrayList<int[]> arrayList=new ArrayList<int[]>(); | |
for(int i=0;i<10;i++) | |
{ | |
int[] intArray=new int[10]; | |
for(int j=0;j<10;j++) | |
{ | |
intArray[j]=i*j; | |
} | |
arrayList.add(intArray); | |
} | |
for(int[] i:arrayList) | |
{ | |
for(int j=0;j<i.length;j++) | |
{ | |
System.out.print(i[j]+","); | |
} | |
System.out.println(""); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void parameterObject() | |
{ | |
ArrayList<ParObject> arrayList=new ArrayList<ParObject>(); | |
for(int i=0;i<10;i++) | |
{ | |
ParObject parObject=new ParObject(); | |
parObject.i=i; | |
parObject.s=String.valueOf(10-i); | |
arrayList.add(parObject); | |
} | |
for(ParObject parObject:arrayList) | |
{ | |
System.out.println("ParObject i="+ parObject.i+",s="+parObject.s); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParObject | |
{ | |
int i; | |
String s; | |
} |
ArrayList使用容易,也很好分辨
如果你要複製一樣的ArrayList,可以用以下的式子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void parameterInt() | |
{ | |
ArrayList<Integer> arrayList=new ArrayList<Integer>(); | |
for(int i=0;i<10;i++) | |
{ | |
arrayList.add(i); | |
} | |
for(int i:arrayList) | |
{ | |
System.out.print(i+","); | |
} | |
System.out.println(); | |
ArrayList<Integer> arrayListCopy=new ArrayList<Integer>(arrayList); | |
for(int i:arrayListCopy) | |
{ | |
System.out.print(i+","); | |
} | |
for(int i=0;i<10;i++) | |
{ | |
arrayList.set(0, 5); | |
} | |
System.out.println(); | |
for(int i:arrayListCopy) | |
{ | |
System.out.print(i+","); | |
} | |
} |
例如下面
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void parameterIntArray() | |
{ | |
ArrayList<int[]> arrayList=new ArrayList<int[]>(); | |
for(int i=0;i<10;i++) | |
{ | |
int[] intArray=new int[10]; | |
for(int j=0;j<10;j++) | |
{ | |
intArray[j]=i*j; | |
} | |
arrayList.add(intArray); | |
} | |
for(int[] i:arrayList) | |
{ | |
for(int j=0;j<i.length;j++) | |
{ | |
System.out.print(i[j]+","); | |
} | |
System.out.println(""); | |
} | |
ArrayList<int[]> arrayListCopy=new ArrayList<int[]>(arrayList); | |
for(int[] i:arrayListCopy) | |
{ | |
for(int j=0;j<i.length;j++) | |
{ | |
System.out.print(i[j]+","); | |
} | |
System.out.println(""); | |
} | |
for(int i=0;i<10;i++) | |
{ | |
for(int j=0;j<10;j++) | |
{ | |
arrayList.get(i)[j]=5; | |
} | |
} | |
for(int[] i:arrayListCopy) | |
{ | |
for(int j=0;j<i.length;j++) | |
{ | |
System.out.print(i[j]+","); | |
} | |
System.out.println(""); | |
} | |
} |
可以發現到當arrayList改變時,arrayListCopy數值也會跟著改變,是因為ArrayList是淺度複製,當參數是單純變數時(例:int, String, boolean),複製的arrayListCopy不會改變,但當參數是陣列 or 物件,ArrayList<int[]> arrayListCopy=new ArrayList<int[]>(arrayList); arrayListCopy 和 arrayList指向的是同一個記憶體位置,所以會造成某個ArrayList改變時,另一個就會跟著改變。
而如果要簡單解決這問題的話,只要做下面的方法就行了:
ArrayList<int[]> arrayListCopy=new ArrayList<int[]>(arrayList);
改成
ArrayList<int[]> arrayListCopy=new ArrayList<int[]>();
{
}
一個一個陣列把他複製下來。
也可以使用 BeanUtils.cloneBean來把它複製。