Java Arrays Module 10 PDF

Summary

This document is a Java lesson on arrays. It covers the concept of arrays, their types (one-dimensional and two-dimensional), and different ways to use arrays in Java programming. The document also includes exercises to help students practice what they learned.

Full Transcript

Java Arrays MODULE 10 Introduction Arrays are a fundamental data structure in Java that provide a way to store multiple values of the same type in a single variable. This lesson introduces the concept of arrays, their importance in programming, and how to utilize them effectively. Students will le...

Java Arrays MODULE 10 Introduction Arrays are a fundamental data structure in Java that provide a way to store multiple values of the same type in a single variable. This lesson introduces the concept of arrays, their importance in programming, and how to utilize them effectively. Students will learn how to work with both one- dimensional and two-dimensional arrays, including their initialization, manipulation, and traversal. Objectives 1. Understand the purpose and importance of arrays in programming. 2. Declare, initialize, and manipulate one-dimensional arrays. 3. Access and modify elements of an array using indexes. 4. Utilize loops to read and write array elements. 5. Work with user input to populate arrays. 6. Understand the concept of two-dimensional arrays and their representation as tables. 7. Declare, initialize, and manipulate two-dimensional arrays. 8. Use nested loops to traverse and modify two-dimensional arrays Arrays Is a collection of multiple values in a Single Variable with the Same Data Type. Arrays in Java are objects and they are useful for storing and managing collections of data. Arrays Is a collection of multiple values in a Single Variable with the Same Data Type. Arrays in Java are objects and they are useful for storing and managing collections of data. Types of Arrays 1. Single Dimensional Array - A single-dimensional array in Java is a linear collection of elements of the same data type 2. Multidimensional Array - A multidimensional array in Java is an array of arrays where each element can be an array itself. It is useful for storing data in row and column format. Arrays Advantages Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently. Disadvantages Size Limit: Arrays have a fixed size and do not grow dynamically at runtime. One dimensional Arrays 1D ARRAYS Syntax of an Array Initializing with values: datatype identifier[] = {value1, value2, value3}; String names[] = {"Juan", "Mark", "John"}; Declaration without values: datatype identifier[] = new datatype [size]; String names [] = new String (4); Index & Element Element: An element is an item or value stored in the array Index: Is a number that represents a position of an element inside an array. Analogy of an Array The egg container is the array while the eggs are the elements, and the numbers represents the index of the eggs. Index vs size Array Size: o This is the total number of elements the array can hold o Defined when you create the array and cannot be changed (arrays are fixed-size in Java) o If you create an array like int[] numbers = new int, the size is 5 o Size starts from 1 (you can't have an array of size 0) Array Index: o This is the position of an element within the array o Always starts from 0 and goes up to (size - 1) o For an array of size 5, valid indexes are 0, 1, 2, 3, and 4. Example String cars [] = new String (10); Example String egg_container [] = new String ( ); What is the size of the array? What are the indexes of the array? What are the values/elements of the array?.length property In Java, the.length property is used to determine the size (number of elements) of an array. It is important to note that.length is not a method but a property of the array. Syntax: arrayName.length Int numbers [] = {1, 12, 32, 45, 65}; System.out.println(numbers.length); Arrays.toString() method It is a part of the java.util.Arrays class, converts an array into a human-readable string representation. This is especially useful for printing arrays, as Java does not directly support printing array contents with System.out.println(). Syntax: arrayName.length Int numbers [] = {1, 12, 32, 45, 65}; System.out.println(Arrays.toString(numbers)); Exercise Initialize an Array that contains 5 names of students. Accessing Array VALUE "Student1" "Student2" "Student3" "Student4" "Student5" INDEX 0 1 2 3 4 You can access an array element by referring to the index number. Accessing an Array Reading an Array Element identifier [index]; names; Assigning an Array Element identifier [index] = value; names = "Juan"; Exercise Print/read the name of student located at index 2. Exercise Assign a new value of the name of student located at indexes: 3 & 4. Then print the Array. Accessing an Array Assigning an Array Element with user input Scanner sc = new Scanner (System.in); names = sc.nextLine(); Exercise Paired Arrays by index create 3 arrays that will hold 4 user's credentials: - email - username - password Then asks the user to enter an int variable and we will use as an index to output the user credentials. for example: email, username and password should belong to the same user. Iterating through an Array (for 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. for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } Iterating through an Array (for each loop) There is also a "for-each" loop, which is used exclusively to loop through elements in array. Syntax: for (datatype new_variable : arrayname) {... } Iterating through an Array (for each loop) example: String[] cars = {"Nissan", "BMW", "Mazda"}; for (String car : cars) { System.out.println(i); } Exercise Display the values/elements of an array by using a for loop and for each loop. Two dimensional Arrays 2D ARRAYS Syntax of a 2D Array Initializing with values: Ex: datatype identifier[][] = { String users[][] = { { value1, value2 }, {"arc01", "pass123"}, { value1, value2 }, {"jd3", "pass124"}, { value1, value2 } {"jr2", "pass125"} }; }; Syntax of a 2D Array Declaring without values: datatype identifier[][] = new datatype [rows_size][cols_size]; Ex: String users [][] = new String ; Accessing a 2D Array INDEX 0 1 String users[][] = { { "user1", "pass1" }, 0 "user1" "pass1" { "user2", "pass2" }, { "user3", "pass3" } "user2" "pass2" 1 }; "user3" "pass3" 2 Accessing a 2D Array Reading Elements identifier [row_index][col_index]; names; Assigning/Writing Elements identifier [row_index][col_index] = value; names = "Juan"; Iterating 2D Arrays (for loop) for(int row= 0; row < arrayName.length; row++){ for(int col = 0; col < arrayName[row].length; col++){ System.out.println( users[row][col] ); } } Iterating 2D Arrays (for loop) for(int row= 0; row < arrayName.length; row++){ for(int col = 0; col < arrayName[row].length; col++){ System.out.println( users[row][col] ); } } Iterating 2D Arrays (for each loop) Syntax (nested for each): for(datatype new_variable1 [] : arrayName){ for(datatype new_variable2 : new_variable1){ System.out.print(new_variable2); } }

Use Quizgecko on...
Browser
Browser