創建實例的類數組

[英]Array of classes which create instances


having Obj class which in his constructor has System.out.println("Hello world!") ;

有Obj類,在他的構造函數中有System.out.println(“Hello world!”);

I create an array of this class using - Obj[] objArray = new Obj[10] ; and nothing printed , means - no instance of Obj has been called . Is there any way to create such array ,but with instances , beyond create them in for loop ?

我用 - Obj [] objArray = new Obj [10]創建了這個類的數組;沒有打印,意味着 - 沒有調用Obj的實例。有沒有辦法創建這樣的數組,但有實例,除了在for循環中創建它們?

4 个解决方案

#1


3  

Well, since you want to know a way apart from using a for loop, you can do this: -

好吧,既然你想知道除了使用for循環之外的一種方法,你可以這樣做: -

Obj[] objArray = {new Obj(), new Obj(), new Obj()};

What happens here is, you are initializing your array reference directly with array elements. Now the type of actual array object is inferred from type of array reference on the LHS.

這里發生的是,您正在使用數組元素直接初始化數組引用。現在,實際數組對象的類型是從LHS上的數組引用類型推斷出來的。

So, with that declaration, an array of size 3 (in the above case) is created, with each index in the array initialized with the instance of Obj class in the given order.

因此,使用該聲明,將創建一個大小為3的數組(在上面的例子中),數組中的每個索引都以給定順序使用Obj類的實例初始化。


A better way that I would suggest is to use an ArrayList, in which case, you have double-braces initialization to initialize your List without for loop. And plus with an added advantage that you can anytime add new elements to it. As it dynamically increasing array.

我建議的更好的方法是使用ArrayList,在這種情況下,你有雙括號初始化來初始化List而不用for循環。此外,還有一個額外的優勢,您可以隨時添加新元素。因為它動態增加數組。

List<Object> list = new ArrayList<Object>() {
    {
        add(new Obj());
        add(new Obj());
        add(new Obj());
    }
};  // Note the semi-colon here.

list.add(new Obj());  // Add another element here.

#2


3  

Answers so far are good and helpful. I'm here just to remind you about

到目前為止,答案很好,很有幫助。我在這里只是為了提醒你

Obj[] objArray = new Obj[10];
Arrays.fill(objArray, new Obj());

Though, this will only assign one reference (to a new Obj()) to all of the elements of array.

但是,這只會為數組的所有元素分配一個引用(對新的Obj())。

#3


1  

When you do Obj[] objArray = new Obj[10] ; You only create an array of references to point to the actual 'Obj` object.

當你做Obj [] objArray = new Obj [10];您只需創建一個引用數組以指向實際的'Obj`對象。

But in your case the actual Obj object is never created.

但在你的情況下,永遠不會創建實際的Obj對象。

for (int i = 0; i < objArray.length; i++) {
objArray[i] = new Obj();
}

Doing above will print the desired.

以上操作將打印所需的內容。

Finally do System.out.println(Arrays.deepToString(objArray)) to print toString() of all the Obj

最后做System.out.println(Arrays.deepToString(objArray))來打印所有Obj的toString()

#4


1  

Obj[] objArray = new Obj[10] ; just creates an array capable of holding 10 Objs. To place Objs into the array you need to either use Rohit's approach or write a simple for loop to initialize the array entries one at a time:

Obj [] objArray = new Obj [10];只需創建一個能夠容納10個Objs的數組。要將Objs放入數組,您需要使用Rohit的方法或編寫一個簡單的for循環來一次初始化一個數組條目:

for (int i = 0; i < 10; i++) {
    objArray[i] = new Obj();
}

Or, without a for loop:

或者,沒有for循環:

int i = 0;
while (i < 10) {
    objArray[i] = new Obj();
    i++;
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/11/24/720c87dc41dc43e84dbbb63ebad5dca8.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com