Try โ€‚โ€‰HackMD

Programming I Lab 8

Lab Titles[7] -> Arrays

The Islamic University of Gaza
Engineering Faculty
Department of Computer Engineering
Author: Mohammed Nafiz ALMadhoun2021/11/19

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.

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.

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.

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.

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:

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:

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.

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.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More โ†’

Task 1 Expected Output

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.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More โ†’

CSV File in Excel

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
End Of Lab 8