Array basics

Question from junk#1089

the question ask for:

user input a number, n then save into array. after that, display the numbers out according to sequence

An array is a fixed size container of elements

int[] numbers = new int[10];

so here I made an array of 10 integers. They will all start at 0, and I can set

numbers[5] = 123;

any number in the array and get

int i = numbers[2];

any number in the array.

But if I try to get a number outside of the bounds of the array (in this case 0-9)

int j = numbers[10];

it will crash. so if a user inputs a number and you want to save that number into an array you need to

  1. Know what "place" in the array you are at
  2. Know whether you can expect a certain number of elements because an array, like I said, is fixed size

thank you so much


<- Index