Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single-Dimensional Arrays chapter6

Similar presentations


Presentation on theme: "Single-Dimensional Arrays chapter6"— Presentation transcript:

1 Single-Dimensional Arrays chapter6
lab8 PROGRAMMING 1 Single-Dimensional Arrays chapter6

2 BEFORE WE START Your Cell Phone Silent Please Keep Quite
Find Your Computer and Switch On Ins.Ebtesam AL-Etowi

3 content Array Basics. Array Type .
To declare array reference variables and create arrays. To initialize the values in an array. To access array elements using indexed variables. To declare, create, and initialize an array using an array initializer. To copy contents from one array to another. To search elements using the linear. Enhanced for Loop (for-each loop). To use the methods in the Arrays class. Ins.Ebtesam AL-etowi

4 Array Basics An array is used to store a collection of data, but often we find it more useful to think of an array as a collection of variables of the same type. Java and most other high-level languages provide a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. Instead of declaring individual variables, such as number0, number1, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and numbers[99] to represent individual variables. Ins.Ebtesam AL-etowi

5 Array Type Ins.Ebtesam AL-etowi

6 Declaring Array Variables
To use an array in a program, you must declare a variable to reference the array and specify the array’s element type. Here is the syntax for declaring an array variable: elementType [ ] arrayRefVar; OR elementType arrayRefVar[ ] ; The elementType can be any data type, and all elements in the array will have the same datatype. For example, the following code declares a variable myList that references an array of double elements. double[ ] myList; Ins.Ebtesam AL-etowi

7 Creating Arrays Unlike declarations for primitive data type variables, the declaration of an array variable does not allocate any space in memory for the array. It creates only a storage location for the reference to an array. If a variable does not contain a reference to an array, the value of the variable is null. After an array variable is declared, you can create an array by using the new operator with the following syntax: arrayRefVar = new elementType[arraySize]; This statement does two things: (1) it creates an array using new elementType[array-Size]; (2) it assigns the reference of the newly created array to the variable arrayRefVar. Example : double[ ] myList = new double[10]; double[ ] myList; OR myList = new double[10]; double myList [ ] = new double[10]; Ins.Ebtesam AL-etowi

8 Example The size of an array cannot be changed after the array is created. Size can be obtained using arrayRefVar.length. For example: myList.length is 10. When an array is created, its elements are assigned the default value of 0 for the numeric. myList holds ten double values, and the indices are from 0 to 9. Ins.Ebtesam AL-etowi

9 Array Size and Default Values
The size of an array cannot be changed after the array is created. Size can be obtained using arrayRefVar.length. For example, myList.length is 10. When an array is created, its elements are assigned the default value of 0 for the numericprimitive data types, '\u0000' for char types, and false for boolean types. Array Indexed Variables The array elements are accessed through the index. Array indices are 0 based; that is, they range from 0 to arrayRefVar.length-1. myList holds ten double values, and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: arrayRefVar[index]; For example, myList[9] represents the last element in the array myList. Ins.Ebtesam AL-etowi

10 Splitting it would cause a syntax error
Array Initializers Java has a shorthand notation, known as the array initializer, which combines in one statement declaring an array, creating an array, and initializing, using the following syntax elementType[ ] arrayRefVar = {value0, value1, ..., valuek}; double[ ] myList = {1.9, 2.9, 3.4, 3.5}; CAUTION which is equivalent to the statements shown below: all in one statement double[ ] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; Splitting it would cause a syntax error Ins.Ebtesam AL-etowi

11 Copying Arrays There are different ways to copy arrays:
1-Use a loop to copy individual elements one by one:. list2 = list1; Often, in a program, you need to duplicate an array or a part of an array. In such cases you could attempt to use the assignment statement (=), as follows: for (int i = 0; i < sourceArray.length; i++) { targetArray[i] = sourceArray[i]; } 2-Use the static arraycopy method in the System class: use the arraycopy method in the java.lang.System class to copyarrays instead of using a loop. The syntax for arraycopy is shown below: arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length); int arry1[ ]={1,2,3}; int arry2[ ]={0,0,0}; System.arraycopy(arry1, 0, arry2, 0, 3); Ins.Ebtesam AL-etowi

12 Passing Arrays to Methods
public class Examplearry { public static void main(String[] args) { int[ ] y = {3, 1, 2, 6, 4, 2}; printArray(y); } public static void printArray(int[ ] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); Arry name pass output Ins.Ebtesam AL-etowi

13 Returning an Array from a Method
1 2 List={1,2,3} result={3,2,1} Ins.Ebtesam AL-etowi

14 Searching Arrays Key is value the user enter to search in arry ,if found the value return the index of value . Ins.Ebtesam AL-etowi

15 Enhanced for Loop (for-each loop)
introduced a new for loop that enables you to traverse the complete array sequentially without using an index variable. For example, the following code displays all elements in the array myList: for (double value: myList) System.out.println(value); Example Output : Ins.Ebtesam AL-etowi

16 The Arrays Class The java.util.Arrays class contains various static methods for sorting and searching arrays, comparing arrays, and filling array elements double[ ] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; java.util.Arrays.sort(numbers); java.util.Arrays.binarySearch(number,3.4); java.util.Arrays.fill(number, 5.3); System.out.println (Arrays.toString(number)); Ins.Ebtesam AL-etowi

17 Explain The Exercise Initialize double an array with the marks of 5 subjects and calculate the average.??? Ins.Ebtesam AL-etowi

18 Exercise Ins.Ebtesam AL-etowi

19 Cont….. Output: Ins.Ebtesam AL-etowi

20 Thank You !


Download ppt "Single-Dimensional Arrays chapter6"

Similar presentations


Ads by Google