Arrays are used to store multiple values in a single variable, allowing for efficient data management. They can be declared with a specific type and accessed or modified using index numbers. Multi-dimensional arrays enable the storage of data in a tabular format, and both types of arrays can be iterated through using loops.
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 ratings0% found this document useful (0 votes)
12 views16 pages
Arrays
Arrays are used to store multiple values in a single variable, allowing for efficient data management. They can be declared with a specific type and accessed or modified using index numbers. Multi-dimensional arrays enable the storage of data in a tabular format, and both types of arrays can be iterated through using loops.
ACCESS THE ELEMENTS OF AN ARRAY • You can access an array element by referring to the index number.
• This statement accesses the value of the first element in cars:
public class Main {
public static void main(String[] args) { String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars[0]); } } CHANGE AN ARRAY ELEMENT • To change the value of a specific element, refer to the index number: public class Main { public static void main(String[] args) { String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; cars[0] = "Opel"; System.out.println(cars[0]); } } CHANGE MORE ARRAY ELEMENT ARRAY LENGTH • To find out how many elements an array has, use the length property:
public class Main {
public static void main(String[] args) { String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; System.out.println(cars.length); } } ARRAYS LOOP • You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run. • The following example outputs all elements in the cars array: public class Main { public static void main(String[] args) { String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (int i = 0; i < cars.length; i++) { System.out.println(cars[i]); } } } MULTI-DIMENSIONAL ARRAYS • A multidimensional array is an array of arrays. • Multidimensional arrays are useful when you want to store data as a tabular form, like a table with rows and columns. • To create a two-dimensional array, add each array within its own set of curly braces: Example: int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} }; ACCESS ELEMENTS • To access the elements of the myNumbers array, specify two indexes: one for the array, and one for the element inside that array. This example accesses the third element (2) in the second array (1) of myNumbers: public class Main { public static void main(String[] args) { int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} }; System.out.println(myNumbers[1][2]); } } CHANGE ELEMENT VALUES LOOP THROUGH A MULTI- DIMENSIONAL ARRAY • You can also use a for loop inside another for loop to get the elements of a two- dimensional array (we still have to point to the two indexes): public class Main { public static void main(String[] args) { int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} }; for (int i = 0; i < myNumbers.length; ++i) { for(int j = 0; j < myNumbers[i].length; ++j) { System.out.println(myNumbers[i][j]); } } } }
Instant download OpenAI API Cookbook: Build intelligent applications including chatbots, virtual assistants, and content generators Henry Habib pdf all chapter