0% found this document useful (0 votes)
11 views

Arrays in Java: Hot Coffee

Arrays in Java allow storing multiple values in a single variable. An array is declared by specifying the data type followed by square brackets, such as String[] cars. Values are then assigned to the array using curly braces and commas to separate each element, like String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}. Arrays can hold primitive types like int as well as object references. The length property returns the number of elements in the array.

Uploaded by

KENNETH LIGUTOM
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 views

Arrays in Java: Hot Coffee

Arrays in Java allow storing multiple values in a single variable. An array is declared by specifying the data type followed by square brackets, such as String[] cars. Values are then assigned to the array using curly braces and commas to separate each element, like String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}. Arrays can hold primitive types like int as well as object references. The length property returns the number of elements in the array.

Uploaded by

KENNETH LIGUTOM
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/ 4

Arrays in Java

HOT COFFEE
The Basics

 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;
The Basics

 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”};
 To create an array of integers, you could write:
 String[] cars = {10, 20, 30, 40};
Summary

 Accessing the Elements of an Array


 Change an Array Element
 Array Length
 To find out how many elements an array has, use the length property.
 Loop through an Array
 Loop Through an Array with For-Each
for (type variable : arrayname){
statements;
}

You might also like