1

I am still new to Java.

I am wondering if there is a quick way to initialize a Java Array with size (n*1 or n*m) and values (0.0, or 1, or "A")provided.

In this post, Initialization of an ArrayList in one line, intilizing Java array is given as follows:

ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");

or

List<String> list = ["A", "B", "C"];

or

List<String> list = List.of("A", "B", "C");

However, none of them is what I want.

I am wondering if it is possible to initialize an Java array of size n*1 or n*m with 0.0 (double array), or 1 (integer array), or "A" (string array).

These initialization will be one/two line code in matlab/python, e.g. In python, ZerosVector = numpy.zeros((n,1)) will return a n*1 vector with zeros. In matlab CellMatrix = cell(10,20); CellMatrix(:) ="A" will return a cell matrix with string "A".

Any equivalent package/implementation in Java for such fast initialization without using loop?

XYZ
  • 352
  • 5
  • 19
  • Do you want an Array (`String[]`) or a List (`List`)? – OH GOD SPIDERS Mar 02 '21 at 14:51
  • @OHGODSPIDERS, sorry, I still do not quite know the difference. But the size is known beforehand, e.g. `n*1`. If `String[]` indicate fixed size, then I mean Array. Any example to initialize an array of double with value 0.0, an array of integer with value 1, and an array of string with "A", size are all `n*m`? – XYZ Mar 02 '21 at 14:54
  • 4
    [This](https://stackoverflow.com/questions/5600668/how-can-i-initialize-an-arraylist-with-all-zeroes-in-java) other question might shed some light for `ArrayLists`. For `arrays` you could look into [Arrays.fill](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#fill(double[],%20double)) – StaticBeagle Mar 02 '21 at 14:58
  • 1
    For `double` the default value is btw. already `0.0`. So to create a double array of length n*m filled with 0.0 all you need is really `new double[n*m]`. – OH GOD SPIDERS Mar 02 '21 at 15:01
  • @StaticBeagle, the link is quite helpful. I did not see this post before the post as I was searching `Array` instead of `ArrayList`. – XYZ Mar 02 '21 at 15:04
  • You may also take a look at [what-is-the-default-initialization-of-an-array-in-java](https://stackoverflow.com/questions/3426843/what-is-the-default-initialization-of-an-array-in-java/3426854) – Eritrean Mar 02 '21 at 15:09
  • Why do the tags include python and matlib if this is a question about java? Language-spamming is not appreciated. – NomadMaker Mar 02 '21 at 16:45

1 Answers1

2

To make it into a one-liner, you can make a simple method, that does it. You can pass the size and value and return the new ArrayList:

static ArrayList<String> populateArrayList(int size, String defaultValue) {
    ArrayList<String> list = new ArrayList<String>();
    for (int i=0; i<size; i++)
        list.add(defaultValue);
    return list;
}

If you wish to use static list - array, then similar code can be applied:

static String[] populateArray(int size, String defaultValue) {
    String[] array = new String[size];
    for (int i=0; i<size; i++)
        array[i] = defaultValue;
    return array;
}

And then to use, just call the method and assign its returned value to a new list:

ArrayList<String> data1 = populateArrayList(100, "Doggo");
String[] data2 = populateArray(100, "Moo-moo");

Default values

As stated in comments, variables of different data types have their default values that are used in arrays, object params etc.

  • for numerics it is 0,
  • for boolean - true,
  • for String and other type it is null

More about default values here

Mike B
  • 2,756
  • 2
  • 16
  • 28