java 我可以在java中用变量命名数组吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4271821/
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-30 05:38:07  来源:igfitidea点击:

Can I name an array with a variable in java?

javaarrays

提问by Johny

for example I want to do this:

例如我想这样做:

String[] arraynames = new String[2];
arraynames[0] = "fruits";
arraynames[1] = "cars";

and now I don't know how to do this

现在我不知道该怎么做

String[] arraynames[0] = new String[100]; // ??????

so that I create a String array called fruits with 100 cells... I know this doesn't work but is there someway to do this?

所以我创建了一个名为 Fruits 的 String 数组,有 100 个单元格……我知道这行不通,但有什么办法可以做到这一点吗?

回答by Yanick Rochon

Use an HashMap

使用 HashMap

Example:

例子:

HashMap<String,String[]> arraynames = new HashMap<String,String[]>();
arraynames.put("fruits", new String[1000]);

// then simply access it with
arraynames.get("fruits")[0] = "fruit 1";

However, may I suggest you replace arrays with ArrayList?

但是,我可以建议您用 替换数组ArrayList吗?

HashMap<String,ArrayList<String>> arraynames = new HashMap<String,ArrayList<String>>();
arraynames.put("fruits", new ArrayList<String>());

// then simply access it with
arraynames.get("fruits").add("fruit 1");

** EDIT**

**编辑**

To have an array of floatvalues instead of strings

拥有一组float值而不是字符串

HashMap<String,ArrayList<Float>> arraynames = new HashMap<String,ArrayList<Float>>();
arraynames.put("fruits", new ArrayList<Float>());

// then simply access it with
arraynames.get("fruits").add(3.1415f);

回答by aperkins

So, you are looking for a doubly indexed array?

那么,您正在寻找一个双索引数组?

Something like:

就像是:

String[][] arraynames = String[2][100];

You have created an array of 2 arrays that contain 100 String elements each in this case.

在本例中,您创建了一个由 2 个数组组成的数组,每个数组包含 100 个字符串元素。

回答by stacker

I hope I get you right, you could something like this:

我希望我说得对,你可以是这样的:

ArrayList arraynames = new ArrayList();

arraynames.add("fruits"); 
arraynames.add("cars");

arraynames.set(0, new ArrayList(100) );

回答by CoolBeans

I am not quite clear on the second line of code here. You said you are trying to create a string array named fruits. This should suffice if I understood this right.

我不太清楚这里的第二行代码。您说您正在尝试创建一个名为fruits 的字符串数组。如果我理解正确,这应该就足够了。

String [] fruits = new String[100]; 

回答by Harima555

you should use a bi-dimensional array like this:

你应该使用这样的二维数组:

String[][] myData = new String[2][100];

now myData is an array with 2 elements, both of them are arrays of "100 cells", then you can use these 2 arrays as follows:

现在 myData 是一个包含 2 个元素的数组,它们都是“100 个单元格”的数组,那么您可以按如下方式使用这 2 个数组:

String[] fruits = myData[0];
String[] cars = myData[1];
fruits[0] = "peach";
cars[0] = "mustang";

回答by Emil

Using Guavalibrary:

使用番石榴库:

ListMultimap<String,String> mappedItems = ArrayListMultimap.create();
mappedItems.put("Fruits","Apple");
mappedItems.put("Fruits","Orange");
mappedItems.put("Fruits","Banana");
mappedItems.put("Cars", "BMW");
mappedItems.put("Cars","Ferrari");
System.out.println(mappedItems);

Output:

输出:

{Cars=[BMW, Ferrari], Fruits=[Apple, Orange, Banana]}

{汽车=[宝马,法拉利],水果=[苹果,橙子,香蕉]}

回答by fastcodejava

You can use one of the following :

您可以使用以下方法之一:

String[][] arraynames = String[2][100];
Map<String, String[]> namesMap = new HashMap<String, String[]>();
List<List<String>> names = new ArrayList<ArrayList<String>()>>();