2016-03-08 70 views
-1

我想知道是否有人能夠在創建我自己的數組列表方法時指向正確的方向。例如,我分配的當前項目不允許我像以下示例一樣免費使用給我的方法。從頭開始創建數組列表

package com.tutorialspoint; 

import java.util.ArrayList; 

public class ArrayListDemo { 
    public static void main(String[] args) { 

    // create an empty array list with an initial capacity 
    ArrayList<Integer> arrlist = new ArrayList<Integer>(5); 

    // use add() method to add elements in the list 
    arrlist.add(15); 
    arrlist.add(22); 
    arrlist.add(30); 
    arrlist.add(40); 

    // adding element 25 at third position 
    arrlist.add(2,25); 

    // let us print all the elements available in list 
    for (Integer number : arrlist) { 
    System.out.println("Number = " + number); 
    } 
    } 
} 

這個例子顯示了add()方法。對於我的項目,我必須自己創建這個方法,並從包中的不同文件中調用它。

+0

你的努力在哪裏? –

+0

@empereur Aiman我不想找任何人爲我完成它,這就是爲什麼我沒有發佈我的作品。我問是否有人有資源/鏈接到我可以瞭解完成我的任務的頁面。無論如何,這是我一直在讀的。看起來我正走在正確的軌道上開始學習。 https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#method_detail – robby

回答

0

我覺得這是一個有趣的問題。我總是對原始水平上的事情如何工作感到好奇。

如果你考慮一下,ArrayList基本上只是一個可以擴展的數組。所以你可以有一個非常大的數組(對於一個ArrayList會佔用大量內存),或者每次添加某個數組時,都會創建一個新的更大的數組並複製這些內容並添加新項目(我認爲性能是上))。

這是我嘗試不使用任何庫:

public class MyArrayList<T> 
{ 
    private T[] asArray; 

    @SuppressWarnings("unchecked") 
    public MyArrayList() 
    { 
     asArray = (T[]) new Object[0]; 
    } 

    public void add(T t) 
    { 
     @SuppressWarnings("unchecked") 
     T[] temp = (T[]) new Object[asArray.length + 1]; 

     // copy everything over to the new array 
     for (int i = 0; i < asArray.length; i++) 
     { 
      temp[i] = asArray[i]; 
     } 

     // add the new element 
     temp[asArray.length] = t; 
     asArray = temp; 
    } 

    public void remove(int index) 
    { 
     if (index < 0 || index >= asArray.length) return; 
     @SuppressWarnings("unchecked") 
     T[] temp = (T[]) new Object[asArray.length - 1]; 

     boolean found = false; 
     // copy everything over to the new element 
     for (int i = 0; i < asArray.length; i++) 
     { 
      // don't copy if the indices are the same 
      if (i == index) 
      { 
       found = true; 
       continue; 
      } 
      temp[i - (found ? 1 : 0)] = asArray[i]; // it's i - 1 after the removed object so then it doesn't leave a gap and it doesn't go over the array's length 
     } 
     asArray = temp; 
    } 

    public T get(int index) 
    { 
     return asArray[index]; 
    } 
} 

讓我很驕傲這個代碼。 :)我認爲Short_Teeth的代碼作弊是因爲該類是一個子類,並且不會添加任何內容。我希望我能幫上忙。

+0

您的方法效率很低,因爲它會在每次添加時生成一個新數組。 也不能你做新的T []而不是新的對象和鑄造? –

+0

我只是想做出有用的東西。我不知道ArrayList實際上是如何做的。 – You

1
import java.util.ArrayList; 

public class MyArrayList<E> extends ArrayList<E>{ 

    private static final long serialVersionUID = -5164702379587769464L; 

    public void newMethod(){ 
     // No implementation 
    } 

} 

這是一個從ArrayList擴展的類,並且一個名爲newMethod()的方法被添加到這個類中。

下面我們在你的情況下調用這個新創建的方法,你必須實現添加到這個新創建的方法。

public class Hello { 

    public static void main(String args[]) { 

     MyArrayList<Integer> myList = new MyArrayList<Integer>(); 

     // It has the ArrayList add() method as this new class extends from ArrayList (all the ArrayList methods are included) 
     myList.add(2); 

     // The newly created method in your case you need to implement the add yourself 
     myList.newMethod(); 
    } 
} 

這也可能是你需要的一個很好的鏈接。

http://www.java2novice.com/java-interview-programs/arraylist-implementation/

我也建議更換您嘗試實現和先解決你的問題,然後問一個具體問題的問題,你做了什麼可能會導致此問題的一個很好的研究後,才(有很多在那裏的資源)。如果你在問這個問題之前做了一些研究,我很肯定你會自己解決所有問題。

希望你會發現這個信息有用。祝你好運。

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--; 
    } 
}