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.
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.
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.
This will create an array of 5 elements, and these elements are 10, 20, 30, 40, and 50.
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.
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.
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:
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:
The matrix is just an array of arrays, but all the arrays in the big array have the same length.
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.
Task 1 Expected Output
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.
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.
Programming
Java
IUG