henk_bae henk_bae - 2 months ago 7
Java Question

i want to make an array int with size 500 and fill

I want to make an array of int with size 500 and fill this in with;


  • 0,1,2,...,499

  • 1,2,3,...,500

  • 500,499,498,...,1

    public static void main(String[] args) {
    int[] numbers = new int[500];

    for(int i=0; i<500; i++) {
    System.out.println(numbers[i]);
    }



I know I need to do this with a for loop but I can't get the right code yet. Can anybody help me please?

Answer

You need to assign a value to each array-element

int[] numbers = new int[500];

for(int i=0; i<500; i++) {
   numbers[i] = i;
   System.out.println(numbers[i]);
}


int[] numbers2 = new int[500];

for(int i=500; i>0; i--) {
   numbers2[i-1] = i;
   System.out.println(numbers2[i-1]);
}
Comments