2013-07-11 25 views
0

我有以下ArrayIntList類,其構造函數定義如下。在最後一個構造函數中,我想要布爾型,如果爲true,則使用該特定元素實例化一個新對象。如果設置爲false,它應該實例化一個具有這麼多容量的新對象。請看我的意思是在這裏的客戶端代碼。它在布爾值爲true時有效。我的類的構造函數不起作用

類文件:

public class ArrayIntList { 
    private int[] elementData; // list of integers 
    private int size;   // current number of elements in the list 

    public static final int DEFAULT_CAPACITY = 100; 

    // post: constructs an empty list of default capacity 
    public ArrayIntList() { 
     this(DEFAULT_CAPACITY); 
    } 

    // pre : capacity >= 0 (throws IllegalArgumentException if not) 
    // post: constructs an empty list with the given capacity 
    public ArrayIntList(int capacity) { 
     if (capacity < 0) { 
      throw new IllegalArgumentException("capacity: " + capacity); 
     } 
     elementData = new int[capacity]; 
     size = 0; 
    } 

    //takes input list and adds to arrayIntList 
    public ArrayIntList(int[] elements) { 
     this(Math.max(DEFAULT_CAPACITY,elements.length*2)); 
     for (int n: elements){ 
     this.add(n); 
     } 
    } 

    //creates an arrayIntlist with data of element 
    public ArrayIntList(int element,boolean notCapacity) { 
     this(); 
     if (notCapacity) { 
     add(element); 
     } 
    //returns the totalCapacity NOT SIZE 
    public int getCapacity() { 
    return elementData.length; 
    } 
} 

客戶端代碼:

public class ArrayIntListExample { 
    public static void main(String[] args) { 
     // Create a new list and add some things to it. 
     ArrayIntList list = new ArrayIntList(); 

     //*** here is my question about ****// 
     ArrayIntList list1 = new ArrayIntList(2, false);//should give [] with capacity of two 
     ArrayIntList list2 = new ArrayIntList(2, true);//should give [2] 
     //*** ****************************** ****// 
     int[] array={2,3,4,5}; 
     ArrayIntList list3 = new ArrayIntList(array); 
     list.add(12); 
     list.add(3); 
     list.add(3); 
     System.out.println("list = " + list); 
     System.out.println("list1 = " + list1); 
     System.out.println("list2 = " + list2); 
     System.out.println("list2 = " + list3); 
     System.out.println("capacity of list1" + list1.getCapacity());//prints 100 but it must be 2 
    } 
} 
+0

您沒有任何代碼,將做到這一點。 – SLaks

+0

是什麼問題?構造函數的代碼看起來不錯(除了一個事實,即你缺少一個'}',我以爲是一個錯字)... – jahroy

+1

什麼是與你的構造函數,你不希望發生的事情? – Kon

回答

0

這應該工作,但是我覺得你的API是混亂..

public ArrayIntList(int element,boolean startElement) { 
    this(startElement ? 1 : element); 
    if (startElement) { 
    add(element); 
    } 
} 

我想你應該刪除此構造函數,而如果用戶想要一個包含特定元素的列表,則讓用戶執行new ArrayIntList(new int[] { 2 })

+2

這是一個可能的解決方案,但IMO描繪了一種設計氣味。 –

+0

是的,這個頁面上的所有代碼都很臭.. –

3

要想讓它表現你想要的方式,我想你想傳遞element給構造與孤int參數:

public ArrayIntList(int element, boolean notCapacity) { 
    this(element); 
    if (notCapacity) { 
     add(element); 
    } 
} 

以前你只是調用this(),這與初始化數組默認容量。如果您手動或通過調試程序遍歷代碼,則可以看到發生這種情況。

還有其他問題,您的設計,但是。除了一個笨拙和令人困惑的接口/ API(通過你的構造函數)之外,數組的容量(即它所能容納的元素的總數量)和它的大小(即元素的數量數組)。

編輯

一個爲什麼你的API是混淆的原因是,你有以下情形:

------------------------------------------------------------------- 
Constructor   | int | boolean | Behavior 
-----------------------+-----+---------+--------------------------- 
(element)    | 2 | x | Array with capacity 2 
(element, notCapacity) | 2 | true | Array with one element (2) 
(element, notCapacity) | 2 | false | Array with capacity 2 
-----------------------+-----+---------+--------------------------- 
  • 你必須做同樣的事情的兩個方面。
  • 您有一個boolean參數被賦予一個負面的名稱。 「容量是錯誤的」比「不容錯誤的」容易。
  • 在引入不一致和混淆的同時,您引入了具有有限值的特徵(使用任意值的一個元素初始化數組)。本質上,notCapacity服務的唯一一點是區分你想要的兩種行爲(初始化容量與初始化一個任意值的元素)。
  • element有兩個,非常不同含義:容量與單個元素被添加到陣列。
  • false更改爲true足以從構造函數中調用非常不同的行爲。
  • 構造函數中的布爾值通常是不透明的,價值有限。從看到像new ArrayIntList(5, true)這樣的調用,意圖是否明顯?
+0

感謝您的回覆。你能否評論爲什麼這是令人困惑的或者構造者是臭的。我知道陣列的容量和大小之間存在差異,但是這是如何影響的。在我添加新元素時確保容量的其餘代碼(我沒有在這裏粘貼)保持謹慎。 –

+1

@ user1988876 - 你的設計聞起來是因爲你有一個構造函數,它接受一個可能意味着兩個不同事物的int。你應該讓構造函數的簽名定義它的作用。換句話說:「_The構造接受一個int作爲參數使用INT作爲容量_... ** ** OR _The construtor接受一個int作爲放慢參數使用INT作爲初始element_」。 int參數不應該有兩種不同的含義,具體取決於您添加到您的構造有關命名布爾... – jahroy

+1

大點一些隨機的布爾值:你應該總是避免使用像_not_和_永遠_燕鷗在布爾變量的名字。否則當你開始處理雙重否定時,事情會變得混亂...... – jahroy

0

如果按照通過代碼很清楚爲什麼行爲發生。

public ArrayIntList(int element,boolean notCapacity) { 
    this(); 
    if (notCapacity) { 
    add(element); 
    } 
} 

這個方法將創建的DEFAULT_CAPACITY一個新的數組,如果它的虛假它只是完成的方法,並返回沒有別的做。雖然,我強烈建議你重新考慮你的階級結構

public ArrayIntList(int element,boolean notCapacity) { 
    this(); 
    if (notCapacity) { 
    add(element); 
    } else { 
    elementData = new int[element]; 
    } 
} 

最簡單的解決辦法簡單地增加一個else語句是這樣。

2

讓我們看一下你的構造:

public ArrayIntList(int element,boolean notCapacity) { 
    this(); 
    if (notCapacity) { 
    add(element); 
    } 
} 

在這條線現在讓我們看看:

this(); 

這條線將調用構造函數不帶參數。該構造是這樣的:

public ArrayIntList() { 
    this(DEFAULT_CAPACITY); 
} 

因此,這將調用帶一個容量的構造,它會通過DEFAULT_CAPACITY給它。所以,加入一些評論原文:

public ArrayIntList(int element,boolean notCapacity) { 

    this(); // initializes this object with a capacity of 100 and no elements 

    if (notCapacity) { 
    add(element); // if notCapacity is true, add the element 
    } 
} 

正如你所看到的,無處你實際使用的「元素」變量,如果notCapacity是假的(這意味着它應該是容量)。

一個非常簡單的此修訂可能是:

public ArrayIntList(int element,boolean notCapacity) { 

    this(notCapacity ? DEFAULT_CAPACITY : element); 

    if (notCapacity) { 
    add(element); 
    } 
} 

不過,我認爲一個更好的設計是沒有這個構造函數的一切,而不是提供下列靜態方法:

public static ArrayIntList createWithElement(int element) { 
    ArrayIntList ret = new ArrayIntList(); 
    ret.add(element); 
    return ret; 
} 

然後調用者有一個乾淨清晰的方法來調用以創建一個具有1個元素的列表。

+0

整潔!我喜歡createWithElement方法。這可能是更好的解決方案 –