JAVA先進-設定(1)

來源:互聯網
上載者:User

標籤:

>Arrays

基本陣列
1.常見的數組產生於main() 函數,數組下標的索引不能超過0到int的範圍
2.當程式試圖訪問數組的第一個或者最後一個資料的時候,會發生ArrayIndexOutOfBoundsException異常。
(相當於鏈表)
3.遺憾的是,數組沒有java源碼。它是由Object對象組成的帶有順序的集合。
聲明/初始化
1.數組的類型有基本類型。Object類型。對於數組的聲明僅僅須要添加[]
2.能夠是int[] test , int []test , int test[] 三種形式。
3.數組的定義必須聲明個數。定義完數組通常使用迴圈或者一個個賦值。
原始類型/物件類型的數組
原始類型的數組維護著資料的順序列表,而對象資料則把資料存在對象中,數組僅僅是引用了對象的地址。
多維陣列
1.像數組引用對象,如果要維護多個連續的數組 這時候就須要多維陣列。


2.多維陣列的初始化也要指定長度。比方2維數組,第1個下標表示維護多少個數組,第2個下標表示維護
數組裡面的個數。


3.多維陣列的值必須由多個維度共同確定。



變數初始化:當類型數組指定個數時,記憶體會為數組指定記憶體 
並給預設的變數,一般依據類型指定,如int指定為0,double為0.0d,String為null;

為方法傳遞數組以及擷取返回值:
當傳遞參數的時候,系統給方法傳遞的是數組的引用,因此你能夠在數組改變其值.

Copying 和 Cloning 數組:
由於數組在初始化已經固定,當你想為數組擴容的時候,就必須使用複製或者複製.
系統為Copy提供了一個工具方法,該方法提供源流的目標。開始位置和複製長度;
當資料長度不夠複製時拋出ArrayIndexOutOfBoundsException
System.arraycopy(original,0, resultArray,0,original.length);
系統相同提供了拷貝方法array.clone();來實現複製。

在類型數組中,拷貝是複製值,而在對象數組中,僅僅是對對象做了一次淺拷貝;

/** * @author Lean  @date:2014-10-16   */public class CopyAndCloneArrays {public static void main(String[] args) throws CloneNotSupportedException {copyingArray();}private static void copyingArray() throws CloneNotSupportedException {int[] array1={1,2,3,4,5};int[] array2={1,2,3,4,5,6,7,8,9};//System.out.println(array1.length+" "+doubleArray(array1).length);//System.out.println(array2//.length+" "+doubleArray(array2).length);Book[] array3={new Book(new Price(20)),new Book(new Price(30))};Book[] array4=doubleArray(array3);array3[0].price.value=18;System.out.println(array3[0].price);System.out.println(array4[0].price);}  static int[] doubleArray(int[] original){int[] resultArray=new int[original.length*2];System.arraycopy(original,0, resultArray,0,original.length);return resultArray;}static Book[] doubleBookArray(Book[] original){Book[] resultArray=new Book[original.length*2];System.arraycopy(original,0, resultArray,0,original.length);return resultArray;}static Book[] doubleArray(Book[] original){return (Book[])original.clone();}}class Price implements Cloneable{public int value;public Price(int value) {this.value = value;}@Overrideprotected Object clone() throws CloneNotSupportedException {Price obj=null;try {obj=(Price)super.clone();} catch (Exception e) {System.out.println(e.getMessage());}return obj;}@Overridepublic String toString() {return value+"";}}class Book implements Cloneable{public Book(Price price) {this.price = price;}public Price price;@Overrideprotected Object clone() throws CloneNotSupportedException {Book obj=null;try {obj=(Book)super.clone();} catch (Exception e) {System.out.println(e.getMessage());}obj.price=(Price) price.clone();return obj;}}



數組引用和相等的推斷:
>當一個變數引用一個數組,比方a引用arrray1,那麼把a賦值給變數b,b相同引用array1. 
>當兩個對象同一時候引用一個數組,那麼推斷他們能夠使用a==b;當兩個對象通過
複製複製得到一個數組,那麼他們引用不同記憶體位址,僅僅能使用equals();

數組與字串:
在C中,總是把char[]數組來表示string,而在Java中,數組跟字串是能夠互轉的
string1.toByteArray()把字串轉化成數組。

new String(byte[])能夠建立字串;


數組反射:

/** * @author Lean  @date:2014-10-16   */public class ArrayReflection {public static void main(String[] args) {//checkArray();//定義數組//reflectNewInstance();//存取值//tryGetSet();}private static void tryGetSet() {int[] data={1,2,3,4};Array.setInt(data,2, 12);Array.setInt(data,4, 12);System.out.println(Array.get(data, 2));}private static void reflectNewInstance() {float[][] bowling=(float[][]) Array.newInstance(float[].class,4);for (int i = 0; i < bowling.length; i++) {bowling[i]=(float[]) Array.newInstance(Float.TYPE, i+1);}System.out.println(bowling[2].length);}private static void checkArray() {final int[] a={1,2,3};Class objClass=a.getClass();if (objClass.isArray()) {Class componentClass=objClass.getComponentType();System.out.println(componentClass.getName());System.out.println(Array.getLength(a));}}}



>The Vector Classes
----Vector----
1.建立Vector能夠指定初始容量和增幅,也能夠把封裝還有一個List,如:
Vector v=new Victor(Arrays.asList(array));
2.Vector遵循隊列的標準
能夠同過add(obj)/addElement()加入到隊列末尾,
能夠通過索引add(index,obj)/insertElement(obj,index)加入索引所在的位置
也能夠將一個Collection隊列所有加入到末尾addAll(list);
3.由於該Vector不能加入原始類型,在新版本號碼的jdk中,系統會為我們自己主動裝箱;列印
Vector使用toString();
4.刪除Vector能夠
通過引用刪除remove(obj)/removeElementAt(index);
通過索引刪除remove(index)/removeElementAt(index);
通過刪除部分removeAll(list)/removeRange(from,to);
所有刪除clear()/removeAllElement();
保留Collection所選 刪除其它 retainAll(list)
5.替換set(index,obj)/setElement(obj,index)
6.查看/設定隊列大小 size()/setSize()
7.儲存容量:容量是用來應對動態隊列的頻繁增減;假使隊列容量太小,就應該在添加
隊列之前使用ensureCapacity(int minCapacity)先擴容;如果容量不夠 一般
添加1倍的容量;與size()不同,size代表當前的個數。容量代表能放多少。當
對隊列操作完成。你能夠調用trimToSize()來最佳化降低空間。
8.Vector的不變性:
如果有一個變數僅做閱讀,為防止變化Collections提供了unmodifiableList(v);
9.對元素進行枚舉
public Enumeration elements()。
while (e.hasMoreElements()) {
process(e.nextElement());
}
擴充AbstractList
Iterator i = v.iterator();
while (i.hasNext()) {
process(e.next());

}


10.多維Vector的迭代取值:
MyType o = (MyType)((Vector)vector.elementAt(3)).elementAt(2);

11.檢驗Vector元素:
推斷存在:contains(obj)/containsAll(list);
索引:indexOf(obj)假設存在2個 得到的是順序的第1個的索引
反向索引:lastIndexOf(obj)如上反向
12.Copy和clone
調用clone()直接複製或者copyInto(array)先轉換成數組再間接複製

13.截取Vector.
public List subList(int fromIndex, int toIndex)

14.使用equals()推斷Vector內容是否相等;


著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。

JAVA先進-設定(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.