0% found this document useful (0 votes)
11 views10 pages

Java Array

Arrays allow us to store multiple values in a single variable. We declare an array by specifying the data type with square brackets, such as String[] or int[]. Values are then inserted into the array using an array literal with curly braces. Individual elements in an array can be accessed or changed by their index number, starting with 0 for the first element. The length property tells us how many elements the array contains.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Java Array

Arrays allow us to store multiple values in a single variable. We declare an array by specifying the data type with square brackets, such as String[] or int[]. Values are then inserted into the array using an array literal with curly braces. Individual elements in an array can be accessed or changed by their index number, starting with 0 for the first element. The length property tells us how many elements the array contains.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Java Array

Java Arrays
Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
To declare an array, define the variable type with square
brackets:

String[] cars;
Java Arrays
We have now declared a variable that holds an array of
strings. To insert values to it, we can use an array literal -
place the values in a comma-separated list, inside curly
braces:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


Java Arrays
To create an array of integers, you could write:

int[] myNum = {10, 20, 30, 40};


Access the Elements of an Array
You access an array element by referring to the index
number.
This statement accesses the value of the first element in
cars:
Access the Elements of an Array

Note: Array indexes start with 0: [0] is the first element. [1]
is the second element, etc.
Change an Array Element
To change the value of a specific element, refer to the index
number:
Change an Array Element
To change the value of a specific element, refer to the index
number:
Array Length
To find out how many elements an array has, use the length
property:

You might also like