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

时间:2021-12-04 02:02:32

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

我想要我的String []数组;是静态但我仍然不知道它的大小。

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

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

6 个解决方案

#1


15  

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

声明它时,您不需要知道数组大小

String[] myArray;

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

但是在初始化时需要知道大小(因为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 ,否则你将*自己实现它(这几乎肯定是更糟糕的选择)。

#2


3  

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.

当然,它有性能下降但它应该是不重要的,除非你对许多不同的数组重复相同。

#3


2  

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?

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

#4


1  

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

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

#5


1  

Using Java.util.ArrayList or LinkedList is the usual way of doing this. With arrays that's not possible as 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);

#6


0  

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] []

样本运行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]

样本运行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]

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

#1


15  

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

声明它时,您不需要知道数组大小

String[] myArray;

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

但是在初始化时需要知道大小(因为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 ,否则你将*自己实现它(这几乎肯定是更糟糕的选择)。

#2


3  

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.

当然,它有性能下降但它应该是不重要的,除非你对许多不同的数组重复相同。

#3


2  

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?

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

#4


1  

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

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

#5


1  

Using Java.util.ArrayList or LinkedList is the usual way of doing this. With arrays that's not possible as 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);

#6


0  

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] []

样本运行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]

样本运行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]

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