filmov
tv
Java ☕ Arrays and Array Lists

Показать описание
Up to this point we have stored data exclusively in variables. Variables are fantastic for storing a single value but what if you want to store more than one value? What if you have a whole list of values? Java does that too.
Java Arrays
A Java array is like a list of variables. Here's is how a Java array is written.
int[] myNumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
In order to create an array, write the datatype as you would for a regular variable. Name your array and write an equals sign. After that, write each item in the array inside the squiggle brackets, separated by commas.
In order to access and use a value from an array, type the name of the array, followed by the index of the item you want. Indexes in programming begin at 0. To access the first item in the array above and store it in an integer variable you would type:
int myNumber = myNumbers[0];
This exercise will help you understand how arrays work.
Java Arrays
A Java array is like a list of variables. Here's is how a Java array is written.
int[] myNumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
In order to create an array, write the datatype as you would for a regular variable. Name your array and write an equals sign. After that, write each item in the array inside the squiggle brackets, separated by commas.
In order to access and use a value from an array, type the name of the array, followed by the index of the item you want. Indexes in programming begin at 0. To access the first item in the array above and store it in an integer variable you would type:
int myNumber = myNumbers[0];
This exercise will help you understand how arrays work.