How to Fill Arrays in Java

How do I fill arrays in Java?

You can also do it as part of the declaration:

int[] a = new int[] {0, 0, 0, 0};

How to fill an array with int numbers using a loop in Java

You should use Arrays.toString, like so:

import java.util.Arrays;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] table = new int[11];
for ( int i = 0; i <=10; i++){
table[i] = i;
System.out.println(Arrays.toString(table));
}
}
}

However, this will print the entire array, as it is being populated:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

If you just want the elements filled so far, it's a little more involved:

import java.util.Arrays;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] table = new int[11];
for ( int i = 0; i <=10; i++){
table[i] = i;
for(int j = 0; j <= i; j++)
{
System.out.print((j == 0 ? "" : ", ") + table[j]);
}
System.out.println();
}
}
}

Output:

0
0, 1
0, 1, 2
0, 1, 2, 3
0, 1, 2, 3, 4
0, 1, 2, 3, 4, 5
0, 1, 2, 3, 4, 5, 6
0, 1, 2, 3, 4, 5, 6, 7
0, 1, 2, 3, 4, 5, 6, 7, 8
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Filling Arrays from user input

Problem

The Arrays.fill() function fills your array entirely up with the same value which you pass as second argument. In your case the value of userNumbersToFillArray

Here an Example:

public static void main(String[] args) 
{
int ar[] = {2, 2, 1, 8, 3, 2, 2, 4, 2};

// To fill complete array with a particular
// value
Arrays.fill(ar, 10);
System.out.println("Array completely filled" +
" with 10\n" + Arrays.toString(ar));
}
}

Outputs:

Array completely filled with 10
[10, 10, 10, 10, 10, 10, 10, 10, 10]

Solution

To solve this you can simply assign inside your loop the user-input value to an array element by its indice.

Write instead of

 Arrays.fill(ints, userNumbersToFillArray);

This here

ints[i] = userNumbersToFillArray;

Here you can read more about Arrays.fill()

  • https://www.geeksforgeeks.org/arrays-fill-java-examples/

fill an array from a specified starting point increasing by n increments

your problem lies in the inner for-loop for (int i = start; start <= end; i++) You increase i, but you are checking start<=end. start does not change.

Further more your code would ouput an array with {6,6,6,6} because with the 2nd for loop you update the array always at the same position. You could just do something like this:

int start = 3;
int end = 6;
int[] myarray = new int[end - start + 1];
for (int a = 0; a < myarray.length; a++) {
myarray[a] = start + a;
}

How do you fill an array with objects from a different class? deteils below

It says you need objects and array. So i guess you wanted something like this.

Person.java

public class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return "Person{" + "name=" + name + ", age=" + age + '}';
}
}

By declaring Person p1 = new Person("Sally",26); you are creating object of class Person. You can use that as many times as you want and create different objects. We use override method toString to print informations about Person. We could also use p1.getName() and p1.getAge()

Main

public static void main(String[] args) {

Person p1 = new Person("Fred", 24);
Person p2 = new Person("Sally", 26);
Person p3 = new Person("Billy", 55);

Person[] people = {p1,p2,p3};

for(Person p : people){
System.out.println(p.toString());
}
}

When using Java Arrays.fill(array,int[] subArray), why subArray share the same memory block?

You would think that Array.fill(dp, new int[2]{2,3}) is equivalent to:

dp[0] = new int[2]{2,3};
dp[1] = new int[2]{2,3};
dp[2] = new int[2]{2,3};
dp[3] = new int[2]{2,3};
dp[4] = new int[2]{2,3};

But no, it's more like:

int[] val = new int[2]{2,3};
dp[0] = val;
dp[1] = val;
dp[2] = val;
dp[3] = val;
dp[4] = val;

You are only ever creating one array in the line Array.fill(dp, new int[2]{2,3}). All subarrays in dp refer to that single array that you created. dp[0] and dp[1] and dp[whatever] all refer to the same array.

This is because when you call a method, all the arguments get evaluated, before the method is run, so new int[2]{2,3} is evaluated before fill is called. fill doesn't "run" the expression new int[2]{2,3} in a loop and assign it to the array. fill doesn't even know what expression you used! Rather, fill only knows the value that the expression new int[2]{2,3} evaluated to - a reference to one newly created int array object. fill then assigns that same object to each index of dp.

Filling rest of Array

Look at this code:

int number =22;
int bits[]= new int[8];
for(int i=0;i<=7;i++){
if(number>0){
bits[7-i]=number%2;
number/=2;
}
else{
bits[7-i]=0;
}
}
for(int i=0;i<=7;i++){
System.out.print(bits[i]);
}

I think, I have resolved your problem.



Related Topics



Leave a reply



Submit