如何在java中定义对象数组?

时间:2022-01-26 00:20:31

In c# I can write below code;

在c#中我可以写下面的代码;

object[] params = new object[4] { "a", 10, "c", true};

What is the Java notation of above code?

上面代码的Java表示法是什么?

7 个解决方案

#1


3  

Object[] param = new Object[]{ obj0, obj1, obj2, ..., objX };

or

Object[] param = { obj0, obj1, obj2, ..., objX};

or

Object[] param = new Object[X+1];
param[0] = obj0 ;
...
param[X] = objX ;

#2


1  

You can do

你可以做

Object[] array = new Object[]{ "a","b"};

Object [] array = new Object [] {“a”,“b”};

  • Note that you do not have to specify the size of array while using this syntax
  • 请注意,使用此语法时不必指定数组的大小

  • The array is filled up with values in the same statement.
  • 数组在同一语句中填充值。

OR

Object[] array2 = new Object[2];

If you do this, then polulate array using

如果你这样做,那么使用polulate数组

array2[0] = "a";
array2[1] = "b";

You can also create anonymous array, which is can be used to pass to a method which takes array argument e.g.

您还可以创建匿名数组,该数组可用于传递给采用数组参数的方法,例如

public void methodTakingArray(Object[] array){
    // perform operations using `array` variable.   
}

and you call the method as methodTakingArray(new Object[]{"a","b"}); so that it can be accessed in the method using array local variable.

并将方法称为methodTakingArray(new Object [] {“a”,“b”});这样就可以在使用数组局部变量的方法中访问它。

#3


0  

Object[] params = {"a","b"}

or

Object[] params = new Object[8];

params[0] = "a";...

#4


0  

Object[] params = new Object[] { "a", "b", "c", "d", "e", "f" , "g", "h" };

Check the difference:

检查区别:

Object instead of object

对象而不是对象

No [8] : you cannot define dimension if you are initializing the array.

否[8]:如果要初始化数组,则无法定义维。

#5


0  

Simple,
Object []params=new Object[]{"a", "b", "c", "d", "e", "f" , "g", "h"};

Simple,Object [] params = new Object [] {“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”};

You can declare any array like this, using the syntax

您可以使用语法声明这样的任何数组

Type [] var_name=new Type[]{/*Values seperated by commas*/x, y, z};

#6


0  

object[] params = new object[8] { "a", "b", "c", "d", "e", "f" , "g", "h" }; // 8 is no need here

object [] params = new object [8] {“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”}; // 8这里不需要

define array as follows

如下定义数组

object[] params = new object[] { "a", "b", "c", "d", "e", "f" , "g", "h" };

Follow this to learn more about Arrays in java.

按照此处了解有关Java中的数组的更多信息。

#7


0  

Very Simple!

As you have started learning Java, I hope you have learned the syntax of Array creation. Array creation in Java is a two step process, unlike C/C++.

当您开始学习Java时,我希望您已经学习了数组创建的语法。与C / C ++不同,Java中的数组创建是一个两步过程。

Since you have worked in C#, it would be easy for you to learn too:

既然您使用过C#,那么您也很容易学习:

Syntax:

    Array_type Array_name[];
    Array_name = new Array_type[Array_size];

So, your solution is:

所以,你的解决方案是:

    Object param[];
    param = new Object[8];

    param[0]='a';
    param[1]='b';
    and so on..

Ofcourse you can combine these two steps into one, by writing:

当然,你可以将这两个步骤合二为一,写成:

    Object param[]={'a','b','c','d','e','f','g','h'};

I hope that helped!

我希望有所帮助!

#1


3  

Object[] param = new Object[]{ obj0, obj1, obj2, ..., objX };

or

Object[] param = { obj0, obj1, obj2, ..., objX};

or

Object[] param = new Object[X+1];
param[0] = obj0 ;
...
param[X] = objX ;

#2


1  

You can do

你可以做

Object[] array = new Object[]{ "a","b"};

Object [] array = new Object [] {“a”,“b”};

  • Note that you do not have to specify the size of array while using this syntax
  • 请注意,使用此语法时不必指定数组的大小

  • The array is filled up with values in the same statement.
  • 数组在同一语句中填充值。

OR

Object[] array2 = new Object[2];

If you do this, then polulate array using

如果你这样做,那么使用polulate数组

array2[0] = "a";
array2[1] = "b";

You can also create anonymous array, which is can be used to pass to a method which takes array argument e.g.

您还可以创建匿名数组,该数组可用于传递给采用数组参数的方法,例如

public void methodTakingArray(Object[] array){
    // perform operations using `array` variable.   
}

and you call the method as methodTakingArray(new Object[]{"a","b"}); so that it can be accessed in the method using array local variable.

并将方法称为methodTakingArray(new Object [] {“a”,“b”});这样就可以在使用数组局部变量的方法中访问它。

#3


0  

Object[] params = {"a","b"}

or

Object[] params = new Object[8];

params[0] = "a";...

#4


0  

Object[] params = new Object[] { "a", "b", "c", "d", "e", "f" , "g", "h" };

Check the difference:

检查区别:

Object instead of object

对象而不是对象

No [8] : you cannot define dimension if you are initializing the array.

否[8]:如果要初始化数组,则无法定义维。

#5


0  

Simple,
Object []params=new Object[]{"a", "b", "c", "d", "e", "f" , "g", "h"};

Simple,Object [] params = new Object [] {“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”};

You can declare any array like this, using the syntax

您可以使用语法声明这样的任何数组

Type [] var_name=new Type[]{/*Values seperated by commas*/x, y, z};

#6


0  

object[] params = new object[8] { "a", "b", "c", "d", "e", "f" , "g", "h" }; // 8 is no need here

object [] params = new object [8] {“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”}; // 8这里不需要

define array as follows

如下定义数组

object[] params = new object[] { "a", "b", "c", "d", "e", "f" , "g", "h" };

Follow this to learn more about Arrays in java.

按照此处了解有关Java中的数组的更多信息。

#7


0  

Very Simple!

As you have started learning Java, I hope you have learned the syntax of Array creation. Array creation in Java is a two step process, unlike C/C++.

当您开始学习Java时,我希望您已经学习了数组创建的语法。与C / C ++不同,Java中的数组创建是一个两步过程。

Since you have worked in C#, it would be easy for you to learn too:

既然您使用过C#,那么您也很容易学习:

Syntax:

    Array_type Array_name[];
    Array_name = new Array_type[Array_size];

So, your solution is:

所以,你的解决方案是:

    Object param[];
    param = new Object[8];

    param[0]='a';
    param[1]='b';
    and so on..

Ofcourse you can combine these two steps into one, by writing:

当然,你可以将这两个步骤合二为一,写成:

    Object param[]={'a','b','c','d','e','f','g','h'};

I hope that helped!

我希望有所帮助!