Tina Comston Comp-111 Week 9 Franklin University |
||||
Week 9 | ||||
Preparation
|
||||
Guided
Learning Activity |
||||
Assignment
Arrays Up to this point we have been working with variables that contain just one value. If we needed more than one of these variables, we would duplicate the variable and perhaps add a number onto the end. For example, if we had 25 students, we might use: String stu1; String stu2; String stu3; etc. through String stu25 This process works OK, but it become tedious. You have to try and determine how many variables you would need - how many students have the potential to be in the class? You also have to write code for each and every variable. if (stu1 != null) { System.out.println(stu1); } You'd have to repeat the above code 25 times, once for each of the variables stu1 through stu25. This can make a program very long, can introduce errors as code is repeated and is challenging to modify as you'd have to make 25 modifications. That's where arrays come in. Rather than defining 25 different variables, instead you define just one variable and indicate to the computer that it is to be repeated 25 times. String stuName[] = new String[25]; This says that we have a variable by the name of stuName that is an array of size 25 and each element in the array will contain a String object. Here's the format for defining an array. dataType arrayName[] = new dataType[length];
To access an element of the array you would use: arrayName[elementPosition] If you wanted to retrieve a value from the array you would use: String tmpName = stuName[7]; - to retrieve the value at element position 7 If you wanted to change an element value in the array you would use: stuName[7] = tmpName; - to change the value at element position 7 It is important to note that the elements in an array are numbered from 0 to the length - 1. If you try to access an element position that is less than 0 or greater than the size-1, you will be outside the boundary of the array and a logic error will result. The FOR loop is commonly used with arrays. for (int i = 0; i < stuName.length; i++) { if (stuName[i] != null) System.out.println(stuName[i]); } The above code will access the elements in the array, one at a time, printing out each element. .length is an attribute of the array. It indicates how many elements are available in the array. YouTube video on arrays https://www.youtube.com/watch?v=o4VzblFTwII
ArrayLists While arrays are a definite improvement over having individual variables, they do have limitations.
Because of these limitations, the developers of JAVA came up with the ArrayList object. The benefits of an ArrayList is:
import Java.Util.ArrayList; The format for declaring an ArrayList is: ArrayList <dataType> arrListName = new ArrayList <dataType> (); Because the ArrayList is an object, a constructor must be called to create the ArrayList object. That is what you are seeing above, with a slight twist. The data type of the elements in the ArrayList is specified within < >.
ArrayList <String> stuName = new ArrayList <String> (); The methods that are available for ArrayList objects are:
for (int i = 0; i < stuName.size(); i++) { System.out.println(stuName.get(i)); } ***We don't need to check to see if the element is null because an ArrayList only contains elements we have added. It will not contain empty elements. YouTube video on ArrayLists https://www.youtube.com/watch?v=e7U-wWdsp78
|
||||
Reflection
Paper
|
||||
Lab 3
|