Arrays are used in java programming to hold multiple variables of the same data type. Instead of having to declare hundreds of variables, you can declare one array and store all the values in that one variable name. It is a very handy and powerful programming tool. An array can be thought of as a numbered LIST of items. You have one list with many items on the list. To declare an array the syntax is as follows: variable type to be stored [ ] arrayName; The square brackets indicate that the variable is an array Examples: int[ ] grades; an array of integers called grades String [ ] names; an array of Strings called names Note that all variables stored in an array must be of the same data type. Just declaring the array does not actually instantiate or create it. You must use the new keyword to actually allocate space for the array in memory. Once an array is created, it can not change size. To create an array that can hold 50 integers in a variable called numbers: int [ ] numbers = new int[50]; To create an array called words to hold 100 String variables: String [ ] words = new String[100]; Note that when arrays of objects such as Strings are instantiated as above, if you try to access an element before something is stored there, you will get a null pointer run-time error. If you create an array of integers like numbers above, an initial value of zero is stored in each array position when it is first instantiated. So after the code int [ ] numbers = new int[50]; is executed, a reference to any index position in numbers would return the value of zero until something else was copied into it by later instructions. Now variables can be stored in the above arrays. To store or read an element in the array, you reference the position in the array. The first position in the array is always at index 0. If an array can store 100 elements, the index positions start at 0 and end at 99 (100 elements). If an array has 20 elements in it, the first element is at position 0 and the last is 19 (Note the last position is always one less than the length of the array.) To put elements into the String array words as written above, the code could look like: 1) words[0] = "Hello"; places the String "Hello" in the first position (index 0) of the array 2) String y = "world"; creates a String and stores it in the variable y. words[1] = y; places the String stored in variable y ("world") as the second element (index position 1) in the array. Accessing works the same way. the code String answer = words[52]; would take the String stored in index position 52 (the 53 element) in the array words and store it in the variable answer. The point in using arrays is to be able to store and access large amounts of information without having to create many variables. There is also a second way to create an array using an initializer list. These are used when you know the variables you want to store in the array. Let's say you want to create an array of integers called scores with the variables 2,10,20,35,46 and 97. An initializer list explicitly lists the elements of an array as it is instantiated. EXAMPLE: int [ ] scores = {2, 10, 20, 35, 46, 97}; Note that the keyword new is not used when using an initializer list. On the right side, instead of listing the number of elements to be stored in the array, the actual elements being stored is listed. Example 2 - An initializer list storing the days of the work week. String [ ] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; To get the size of an array, every array object has a field associated with it called length. So using the examples above, the value returned from scores.length = 6 (index positions would be 0 to 5) week.length = 5 (index positions would be 0 t0 4) int num = words.length; would store 100 in num since 100 is the number of elements in the String array words. Be mindful that length returns the actual size of an array, but since the index starts at 0, the position of the last element is ALWAYS ONE LESS THAN THE size of the array. Assignment: For the code below: Do questions 1 to 5 and send your answers to me via email by Wednesday, 4/22/20. The code for problems 6-10 is due by next Monday, 4/27/20. Put all the answers for problems 6 to 10 in the same program. You don't have to write a separate class for each loop. Comments are closed.
|
Archives
May 2020
Categories |