How to initialize all the elements of an array to any specific value in java

JavaArrays

Java Problem Overview


In C/C++ we have memset() function which can fulfill my wish but in Java how can i initialize all the elements to a specific value? Whenever we write int[] array=new int[10]; , this simply initialize an array of size 10 having all elements equal to zero. I just want to change this initialization integer for one of my array. i.e. I want to initialize an array which has all elements equal to -1. Otherwise I have to put a for loop just after initialization, which ranges from index 0 to index size-1 and inside that loop, I am assigning element to -1. Below is the code for more understanding-

	int[] array = new int[10];
	for (int i = 0; i < size; i++) {
		array[i] = -1;
	}

Am i going correct? Is there any other way for the same?

Java Solutions


Solution 1 - Java

If it's a primitive type, you can use Arrays.fill():

Arrays.fill(array, -1);

[Incidentally, memset in C or C++ is only of any real use for arrays of char.]

Solution 2 - Java

There's also

int[] array = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};

Solution 3 - Java

It is also possible with Java 8 streams:

int[] a = IntStream.generate(() -> value).limit(count).toArray();

Probably, not the most efficient way to do the job, however.

Solution 4 - Java

You could do this if it's short:

int[] array = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};

but that gets bad for more than just a few.

Easier would be a for loop:

  int[] myArray = new int[10];
  for (int i = 0; i < array.length; i++)
       myArray[i] = -1;

Edit: I also like the Arrays.fill() option other people have mentioned.

Solution 5 - Java

Solution 6 - Java

Have you tried the Arrays.fill function?

Solution 7 - Java

You can use Arrays.fill(array, -1).

Solution 8 - Java

Evidently you can use Arrays.fill(), The way you have it done also works though.

Solution 9 - Java

Using Java 8, you can simply use ncopies of Collections class:

Object[] arrays = Collections.nCopies(size, object).stream().toArray();

In your case it will be:

Integer[] arrays = Collections.nCopies(10, Integer.valueOf(1)).stream().toArray(Integer[]::new);
.

Here is a detailed answer of a similar case of yours.

Solution 10 - Java

For Lists you can use

Collections.fill(arrayList, "-")

Solution 11 - Java

Arrays class in java.utils has a method for that.

Arrays.fill(your_array, value_to_fill);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRavi JoshiView Question on Stackoverflow
Solution 1 - JavaOliver CharlesworthView Answer on Stackoverflow
Solution 2 - JavaGilbert Le BlancView Answer on Stackoverflow
Solution 3 - JavaAlexeyView Answer on Stackoverflow
Solution 4 - JavaDanationView Answer on Stackoverflow
Solution 5 - JavaAravind YarramView Answer on Stackoverflow
Solution 6 - JavaSam GoldbergView Answer on Stackoverflow
Solution 7 - Javauser973999View Answer on Stackoverflow
Solution 8 - JavaRenuzView Answer on Stackoverflow
Solution 9 - Javahd84335View Answer on Stackoverflow
Solution 10 - JavaMohan MunisifreddyView Answer on Stackoverflow
Solution 11 - Java0xdeeeView Answer on Stackoverflow