jack zhang
jack zhang

Reputation: 151

How to store numbers in an array?

I just learned the array function in Java and now I wanted to store numbers from 1-19 in an array but don't know the right way to do it without the userinput function. Here is what I got, can you tell me if it is the right way to store number in array?

public class ArrayQuestion1 {
   public static void main(String[] args) {
       int a=0;
       int array[] = new int [20];
       for ( array[a]=1; array[a]<=19; array[a]++){
          System.out.println(array[a]);
       }
   }
}

Upvotes: 1

Views: 85913

Answers (6)

HenryDev
HenryDev

Reputation: 4993

If you want to add consecutive numbers you can use a simple for loop and to see them on the screen you can just iterate your array. That is all. Hope this can help you!

class Main {

public static void main(String[] args) {
int a=0;
int array[] = new int [20];
for(int i = 0 ; i < array.length ; i++){
    array[i] = i;
 }
 for(int x : array){
   System.out.println(x);
  }
 }
}

Upvotes: 1

user4854920
user4854920

Reputation:

 public static void main(String[] args) {
     int array[] = new int[20];
     for (int i = 1; i < array.length; i++){
         array[i] = i;
     }

    //To print all the elements in the array.
    for(int j=1; i< array.length; j++){
        system.out.println(array[j]);
    }   
 }

You can insert into the array using the above method and can view the contents of array also.

Upvotes: 1

singhakash
singhakash

Reputation: 7919

To store userinputs to int array you can do

int array[] = new int [20];
Scanner scanner=new Scanner(System.in);

for ( i=0; i<array.length; i++){
    array[i]=scanner.nextInt();
}

If you want to store number from 0 to 19 you can do

int array[] = new int [20];

for ( i=0; i<array.length; i++){
    array[i]=i;
}

Upvotes: 1

Gabriele Mariotti
Gabriele Mariotti

Reputation: 365008

To use an array you have to declare it.

int array[] = new int [19];

If you want 19 numbers, then use an array with 19 elements.

Then you have to populate each number in the array. To obtain it, just use an index in your array:

array[index] = value

For example:

   for ( int i=0; i<array.lenght; i++){
      array[i] = xx;
   }

Pay attention. The first index in your array is 0 (not 1)

Upvotes: 0

QBrute
QBrute

Reputation: 4541

You would do something like this to fill your array with consecutive numbers from 0-19

public class ArrayQuestion1 {
    public static void main(String[] args) {
        int array[] = new int [20];
        for (int a = 0; a < array.length; a++){
            array[a] = a;
        }
    }
}

Upvotes: 3

Hiren patel
Hiren patel

Reputation: 971

If you don't want user input for array , you have to store numbers manually in the array like ,

int a=0;
int array[] = new int [20];
for ( a=1;a<=19; a++){

array[a]=a;

}

above code will store 0 to 19 in your array . than you can use below for loop to print it

for ( a=1; a<=19; a++){
    System.out.println(array[a]);
}

Upvotes: 0

Related Questions