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