如何声明未知大小的字符串数组[](JAVA)

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

How to declare string array[] of unknown size (JAVA)

javaarraysstring

提问by tin

I want my String[] array;to be static but I still don't know it's size.

我希望我String[] array;是静态的,但我仍然不知道它的大小。

Is there any way to declare string array of unknown size? As much as possible I don't want to use ArrayList

有没有办法声明未知大小的字符串数组?我尽可能不想使用 ArrayList

采纳答案by Jiri Tousek

You don't need to know the array size when you declareit

你并不需要知道数组的大小,当你宣布

String[] myArray;

but you do need to know the size when you initializeit (because Java Virtual Machine needs to reserve a continuous chunk of memory for an arrayupfront):

但是在初始化时确实需要知道大小(因为Java 虚拟机需要预先为数组保留一块连续的内存):

myArray = new String[256];

If you don't know what the size will need to be in the moment you initialize it, you need a List<String>, or you'll be forced to make your own implementation of it (which is almost certainly worse option).

如果您在初始化时不知道需要多大的大小,则需要一个List<String>,否则您将被迫自己实现它(这几乎肯定是更糟糕的选择)。

回答by steveman

No, it needs to be declared, and thus have a length before you can set elements in it.

不,它需要声明,因此在您可以在其中设置元素之前有一个长度。

If you want to resize an array, you'll have to do something like: Expanding an Array?

如果要调整数组大小,则必须执行以下操作:展开数组?

回答by MiOnIs

The listis better for manipulation of an "array" for which you don't know length.

列表更适合操作您不知道长度的“数组”。

回答by huseyin tugrul buyukisik

String [] array = new String[1];

it will be garbage collected later after you init with a real array n elements.

在使用真实数组 n 个元素进行初始化后,它将在稍后被垃圾收集。

array = new String[n];

ofcourse it has a performance decrease but it should be non-importance unless you repeat same for many different arrays.

当然,它会降低性能,但除非您对许多不同的阵列重复相同的操作,否则它应该不重要。

回答by Vaibhav Walke

Using Java.util.ArrayList or LinkedList is the usual way of doing this. With arrays that's not possibleas I know.

使用 Java.util.ArrayList 或 LinkedList 是执行此操作的常用方法。我所知,使用数组是不可能的

Example:

例子:

List unindexedVectors = new ArrayList();

列出 unindexedVectors = new ArrayList();

unindexedVectors.add(2.22f);

unindexedVectors.add(2.22f);

unindexedVectors.get(2);

unindexedVectors.get(2);

回答by Ray Mulgrew

My apologies to the nay-say-ers, this can be done easily.

我向反对者道歉,这很容易做到。

import java.util.Random;

public class Roll_2 {
static Random random = new Random();

public static void main(String[] args) {
int[] variableArray1 = init(random.nextInt(9)); // random size
int[] variableArray2 = init(random.nextInt(9)); // random size
int[] variableArray3 = init(random.nextInt(9)); // random size

randomaize(variableArray1); // randomize elements
randomaize(variableArray2); // randomize elements
randomaize(variableArray3); // randomize elements

print(variableArray1);  // print final
print(variableArray2);  // print final
print(variableArray3);  // print final
}
private static int[] init(int x) {
int[] arr = new int[x];
return arr;
}
private static void print(int[] body) {
System.out.print("[");
for (int i=0;i<body.length;i++) {
System.out.print(body[i]);
if (i<body.length-1) System.out.print(" ");
}
System.out.println("]");
}
private static void randomaize(int[] body) {
for (int i=0;i<body.length;i++) {
body[i] = random.nextInt(9);
}
}
}

Sample Run 1: [1 7 2] [5 2 8 6 8 3 0 8] []

Sample Run 2: [2 5 6 8 0 7 0 6] [0 0 1] [2 1 2 1 6]

Sample Run 3: [8 3 3] [7] [1 3 7 3 1 2]

样品运行 1:[1 7 2] [5 2 8 6 8 3 0 8] []

样品运行 2:[2 5 6 8 0 7 0 6] [0 0 1] [2 1 2 1 6]

样品运行 3:[8 3 3] [7] [1 3 7 3 1 2]