java 在java中可以有一个变量数组吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2649573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:12:13  来源:igfitidea点击:

in java can one have an array of variables?

javavariablesarrays

提问by David

in java can you have an array of variables?
if so what is the syntax?

在java中你可以有一个变量数组吗?
如果是这样,语法是什么?

here's an example if your confused:

如果您感到困惑,这里有一个示例:

varint[] ArrayOfVariablesThatAreInts = new varint[#] 

or

或者

var[] ArrayofVariables = new var[#]

is something like this legal?

这样的事情合法吗?

回答by Prine

Yes you can use:

是的,您可以使用:

Foo[] arrFoo = new Foo[10];
arrFoo[0] = new Foo();
..

Or if you dont want to define a fix size, you can use an ArrayList:

或者,如果您不想定义固定大小,则可以使用 ArrayList:

List<Foo> arrFoos = new ArrayList<Foo>();
arrFoos.add(new Foo());
..

回答by OscarRyz

Not really.

并不真地。

You can have an array of int valuesthough:

你可以有一个 int数组:

int[] intArray = new int[100]; // array to hold 100 int's

But you can't use them as variables, you'll have to use them as values.

但是您不能将它们用作变量,您必须将它们用作

intArray[0] = 512;// set's the first element in the array to 512


int someIntVariable = intArray[0]; // get the first element in the array ( 512 ) .

Arrays are fixed size ( once allocated can't shrink or grow ) to do that you should use a List( variable size ) of ints:

数组是固定大小的(一旦分配不能缩小或增长),你应该使用一个List(可变大小)整数:

List<Integer> list = new ArrayList<Integer>(); // Integer is a wrapper for int 
list.add(512);
list.add(1024);

int x = list.get(0);// get the first element in the list ( 512 ) 

回答by Victor

I'm not sure if this is what you mean by a array of variables but see if this is what your looking for.

我不确定这是否是您所说的变量数组的意思,但看看这是否是您要找的。

import java.util.ArrayList;

导入 java.util.ArrayList;

public class StackQuestion {

公共类 StackQuestion {

private static int random1 = 1;
private static int random2 = 2;

public static void main(String [] args){

    ArrayList a1 = new ArrayList();
    a1.add(random1);
    a1.add(random2);

    System.out.println(a1.get(0));
    System.out.println(a1.get(1));


}

}

}

回答by Binary Nerd

To create an array of intsfor example you would use:

ints例如,要创建一个数组,您将使用:

int[] array = new int[size];

Where sizeis how big you want the array to be.

其中size是您希望数组有多大。

回答by jebcor

Sure. You can do something like: String[] arrayOfStrings = new String[10];

当然。您可以执行以下操作: String[] arrayOfStrings = new String[10];

回答by Fazal

In java you can have arrays of a specific type (like string, int or any object). You can use this array positions to store variables if you want to store them in an array. Or alternatively you can create an object array which can store variables of different types. The length of the array needs to be pre defined and if that doesn't suit you,you can use any cooleciton like ArrayList

在 java 中,您可以拥有特定类型的数组(如字符串、整数或任何对象)。如果要将变量存储在数组中,则可以使用此数组位置来存储变量。或者,您可以创建一个对象数组,该数组可以存储不同类型的变量。数组的长度需要预先定义,如果这不适合你,你可以使用任何像 ArrayList 这样的cooleciton