9

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

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

qqrrrr
  • 575
  • 2
  • 8
  • 22
  • 10
    ArrayList is just the thing you need for this purpose. I do not see any reason to avoid ArrayList. – J Fabian Meier Aug 30 '16 at 11:53
  • Why not List? Far better than array of the size changes. – duffymo Aug 30 '16 at 11:53
  • 3
    Check out dynamic array http://stackoverflow.com/questions/1647260/java-dynamic-array-sizes – Thirumal Aug 30 '16 at 11:53
  • 1
    Arrays have a fixed size which must be known when the array is created. If you need something to hold a variable number of elements, use a collection class such as `ArrayList` instead. See tutorial: [Collections](https://docs.oracle.com/javase/tutorial/collections/) – Jesper Aug 30 '16 at 11:54
  • The simplest option is to use an ArrayList. Alternatively you can use an empty String[] to start with and copy it into a new String[] each time the size changes. – Peter Lawrey Aug 30 '16 at 12:07

6 Answers6

28

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):

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).

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
3

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?

Community
  • 1
  • 1
steveman
  • 139
  • 7
3
String [] array = new String[1];

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

array = new String[n];

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

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
1

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

MiOnIs
  • 125
  • 12
1

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

Example:

List unindexedVectors = new ArrayList();

unindexedVectors.add(2.22f);

unindexedVectors.get(2);

Vaibhav Walke
  • 445
  • 3
  • 6
-1

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]

muneebShabbir
  • 2,500
  • 4
  • 29
  • 46
  • 1
    That were different runs, thus different arrays were created. Try to access element with index greater than array size, and you will get an `IndexOutOfBoundsException`. – coffman21 May 18 '21 at 10:22