2014-03-31 67 views
5

我想實現我自己的ArrayList而不使用Java集合用於實踐目的。在這個階段,我想實現兩個主要的方法,添加(E)和get(int)tp得到這個想法。我的代碼如下。但是我遇到的幾個問題:在不使用集合的情況下實現自己的ArrayList <>

  1. 線「返回(E)MYDATA的[指數]」警告「類型安全:未選中從對象轉換爲E」的問題。我如何解決這個問題
  2. ArrayList.add(T)的Java 7實現返回一個布爾值。在什麼情況下,add()必須返回false。在什麼邏輯下它返回false,何時返回true?
  3. 我在哪裏可以找到Java 7中執行的ArrayList

PS的源代碼。請不要回答問題3,並將我轉到一,二的蔗糖代碼!

import java.util.Arrays; 

public class MyArrayList<E>{ 
    private final int DEFAULT_SIZE=2; 
    private Object[] myData = new Object[DEFAULT_SIZE]; 
    private int actSize=0; 

    public boolean add(E data){ 
     if (actSize>=myData.length/2){ 
      increaseSize(); 
     } 
     myData[actSize++] = data; 
     return true;//when can it be false? 
    } 

    private void increaseSize()throws RuntimeException{ 
     myData = Arrays.copyOf(myData, myData.length*2); 
    } 

    public E get(int index) throws RuntimeException{ 
     if (index >= actSize){ 
      throw new IndexOutOfBoundsException(); 
     } 
     return (E) myData[index]; 
    } 

    public static void main(String[] args) { 
     MyArrayList<String> arList = new MyArrayList<>(); 
     arList.add("Hello"); 
     arList.add("Bye bye!"); 
     System.out.println(arList.get(1));// prints Bye bye! which is correct 

    } 
} 
+0

如果你下載[JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html),你可以找到源代碼在$ JDK_HOME/src.zip。如果您沒有JDK,則可以使用http://grepcode.com/這樣的網站。 – Jeffrey

+0

您是否正在實施*列表? – aliteralmind

+0

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/ArrayList.java?av=f –

回答

3

線 「返回(E)MYDATA的[指數]」 警告 「類型安全:從Object 未經檢查的強制轉換爲E」 的問題。我怎樣才能解決這個

禁止警告

@SuppressWarnings("unchecked") 

的Java 7的實施ArrayList.add(T)返回boolean。 在什麼情況下add()必須返回false。在 邏輯下它返回false,何時返回true

javadoc

返回true(由Collection.add(E)指定)

它總是返回true

我在哪裏可以通過簡單的搜索

的Java ArrayList的源代碼

找到Java 7中執行的ArrayList

的源代碼在你的JDK安裝的src.zip存檔或在網上找到

+0

@nachokk大多數情況下創建一個通用數組比其值得的更麻煩。 – Jeffrey

+0

@nachokk他們可以,但創建一個通用數組也是一個麻煩。 –

0
  1. 我不認爲y ou可以避免使用通用類型的Type安全警告。如果它真的困擾你,你可以添加@SupressWarnings("unchecked")
  2. 嗯,我不確定這個,但是Java的Android實現不完全一樣,它說「它總是返回true」。這對我來說很奇怪,但這裏是鏈接:http://developer.android.com/reference/java/util/ArrayList.html#add(E)
  3. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java,但絕對下載源並檢查出來。
2

線 「返回(E)MYDATA的[指數]」 預警問題 「類型安全:未選中從對象轉換爲E」。我如何解決這個問題?

因爲您正在使用泛型數組,所以您總是會有這個未經檢查的轉換警告。泛型和數組don't really mix那麼好,但更好的慣例是必須連接到陣列反正泛型類型:

private E[] myData = (E[]) new Object[DEFAULT_SIZE]; 

你總是可以添加@SuppressWarnings("unchecked")到外地本身獲得的警告消失。

@SuppressWarnings("unchecked") 
private E[] myData = (E[]) new Object[DEFAULT_SIZE]; 

的Java 7的實施ArrayList.add(T)的,返回一個布爾值。在什麼情況下,add()必須返回false。在什麼邏輯下它返回false,何時返回true?

這是一個有趣的問題。通常情況下,人們會認爲在add限制來自Collections#add

集合支持此操作可能對可加入本集合哪些元素的限制。特別是,一些集合將拒絕添加空元素,而其他集合將對可能添加的元素的類型施加限制。集合類應在其文檔中明確指定可添加哪些元素的任何限制。

...但是,因爲ArrayList的特殊之處在於它的設計總是擴大空間時,它的即將耗盡,它會(理論上)總是能夠在補充一下。所以,應該總是返回true

從哪裏可以找到ArrayList的Java 7實現的源代碼?

​​通常是一個很好的資源。如果您使用源代碼下載JDK,還可以在src.zip中找到它。

-1

我在評論中做了很少的解釋,因爲它很清楚理解。

public class MyArrayList<E extends Object> { 

    private static int initialCapacity = 5; 
    private static int currentSize; 
    private Object[] myArrayList = {}, temp = {}; 

    private static int currentIndex = 0; 

    public static void main(String[] args) { 
     MyArrayList arrList = new MyArrayList(); 
     arrList.add("123"); //add String 
     arrList.printAllElements(); 
     arrList.add(new Integer(111)); //add Integer 
     arrList.printAllElements(); 

     arrList.add(new Float("34.56")); //add Integer 
     arrList.printAllElements(); 

     arrList.delete("123"); 
     arrList.printAllElements(); 

     arrList.delete(123); 
     arrList.printAllElements(); 
     arrList.delete(123); 

     arrList.printAllElements(); 

    } 

    public MyArrayList() { //creates default sized Array of Objects 
     myArrayList = new Object[initialCapacity]; //generic expression 

     /* everytime I cross my capacity, 
    I make double size of Object Array, copy all the elements from past myObject Array Object 
     */ 
    } 

    public MyArrayList(int size) { //creates custom sized Array of Objects 
     myArrayList = new Object[size]; 
    } 

    public void add(Object anyObj) { 
     //add element directy 
     myArrayList[currentIndex] = anyObj; 
     currentSize = myArrayList.length; 
     currentIndex++; 
     if (currentIndex == currentSize) { 
      createDoubleSizedObjectArray(currentSize); 
     } 
    } 

    //print all elements 
    public void printAllElements() { 
     System.out.println("Displaying list : "); 
     for (int i = 0; i < currentIndex; i++) { 
      System.out.println(myArrayList[i].toString()); 
     } 
    } 

    private void createDoubleSizedObjectArray(int currentSize) { 
     temp = myArrayList.clone(); 
     myArrayList = new MyArrayList[2 * currentSize]; //myObject pointer big size data structure 

//   myObject = temp.clone(); //probably I can do this here as well. Need to check this 
     System.arraycopy(temp, 0, myArrayList, 0, currentSize); 

    } 

    void delete(Object object) { 
     //if already empty 
     if (currentIndex == 0) { 
      System.out.println("Already empty!"); 
      return; 
     } 
     //you don't need to delete anything. I can simply override the storage 
     currentIndex--; 
    } 
} 
+0

這不回答任何OP的問題。 – shmosel

相關問題