--- description: In this lab, we are going to focus on arrays, and we will understand the concept of index more, then we will talk briefly about two-dimensional arrays. --- <h1 style='border: none'><center>Programming I Lab 8</center></h1> <h2 style='border: none'><center>Lab Titles[7] -> Arrays</center></h2> <h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5> <h6>Author: Mohammed Nafiz ALMadhoun<span style="float:right">2021/11/19</span></h6> --- <p style='text-align:justify'> In this lab, we are going to focus on arrays, and we will understand the concept of index more, then we will talk briefly about two-dimensional arrays. </p> ## Lab Objectives - Understand arrays. - Be able to use arrays. - Understand the multidimensional arrays. ## Arrays ### Defining an Array An array is just a variable, to define an array in Java all you need to do is to add the postfix [] to the data type you want to make an array of it. After that you will need to initialize the array (allocate the required memory for the elements), to initialize the array you will need to know the number of elements you want to store in it. ```java= elementType[] arrayRefVar = new elementType[arraySize]; ``` So the elementType could be anything you want (e.g. int, double, char, or String). You can also initialize the array with the data in it, so these values or data will be stored in the array after the creation. ```java= int[] array = {10, 20, 30, 40, 50}; ``` This will create an array of 5 elements, and these elements are 10, 20, 30, 40, and 50. ### Accessing an element in the array If you want to access an element in an array, you can just write the array variable name followed by the index of the element inside brackets. ```java= int[] array = {10, 20, 30, 40, 50}; System.out.println (array[0]); ``` **Note:** You can get the length of an array by writing `array.length`, note here that length is just a variable the has a value, not like the method length() in the String class. ## Two Dimensional Arrays The concept of arrays will ramin the same even if you have 100 diminitions in the array, you should think about as an array of array, so you can write: ```java= int[][] arrayOfArrays = new int[10][]; arrayOfArrays[0] = new int[]{1,2,3,4,5}; System.out.println(arrayOfArrays[0][1]); ``` Which will print `2`, we defined an array that contains 10 arrays, each array could contain any number of elements, so we put the array 1, 2, 3, 4, and 5 in the first element of the array (which is an array!). You could define a matrix easily by writing: ```java= int[][] matrix = new int[10][5]; ``` The matrix is just an array of arrays, but all the arrays in the big array have the same length. <div style="page-break-after: always;"></div> ## Lab Tasks ### Task 1: Students Search Engine! In this task, you should let the user enter the student names, and ids, then after filling the data, the user could search about a student using his student id. <center> ![Task 1 Expected Output](https://i.imgur.com/kAh04VR.png =500x) Task 1 Expected Output </center> ### Task 2: Upgrade your search engine! In this task, you should use your first task code, so that the program will read the students ids and names from a `CSV` file, here is a sample of the file. <center> ![CSV File in Excel](https://i.imgur.com/7QmmnYS.png =200x) CSV File in Excel </center> You can create the file itself using Excel or LibreOffice Calc, and save it as CSV file. The CSV file is just a text file, the file contains a line for each row, and it separates each column by `,`. **Note:** You can use the `split(",")` method in the String class. ``` Mohammed ALMadhoun,120141193 Test Test,120201234 Test 2,120212222 Test 3,120203333 Test 4,120204444 ``` ###### tags: `Programming` `Java` `IUG` <center>End Of Lab 8</center>