Presentation is loading. Please wait.

Presentation is loading. Please wait.

Grouped Data Arrays, and Array Lists.

Similar presentations


Presentation on theme: "Grouped Data Arrays, and Array Lists."— Presentation transcript:

1 Grouped Data Arrays, and Array Lists

2 Arrays An array is an object that can hold several values of the same type. This can be useful in a number of ways: test scores(an array of int values), class list(an array of Strings), etc. An array can hold any object type or fundamental variable type, as long as all elements are the same type.

3 Creating an array The brackets [ ] are the symbol for an array.
They can go before or after the name of the array variable. Since arrays are objects, you also need the keyword new. So: double arrayOne[ ] = new double[20]; and: int[] arayTwo = new int[20]; are both acceptable.

4 Index and Elements An array holds a specific, unchangeable number of elements. The number in the second set of braces is the total number of elements in the array. The index is an int value that lets you refer to an element of the array The first index is 0 and the last is one less than the length of the array.

5 Assigning Values Make an array of ints called arrayThree that holds 2 elements. Now we will assign values: arrayThree[0]=5; arrayThree[1]=10; int number = arrayThree[1]; int arrayThree[] = new int[2]; What is the value of number?

6 Assigning at Declaration
You can give an array values when you create it: double arrayFour[] = {2.5, 6.3, -.07}; The braces tell the computer that you will be supplying elements Don’t forget the first Set of brackets. How ever many elements you type, separated by commas, is the size of the array. This is the only time you do not use the keyword new

7 Assigning a null array If you want to make an array, but are unsure of the number of elements you want in it, you can create a null array, and give the number of elements later. boolean arrayFive[]; Some point later: arrayFive = new boolean[5]; Notice that if you want to use the array, you don’t need brackets.

8 Arrays as Parameters Remember that since arrays are objects, if a method mutates another reference, the actual value changes. Also, if you want to send the entire array into a method as a parameter, remember you do not use the brackets, just the name of the array reference variable.

9 length The array class has a public static instance variable called length. Since it is public you can reference it from other classes. Since it is static, you cannot change it. char arraySix []= new char[7]; int arSixLength = arraySix.length; arSixLength would have the value 7. Notice there are no ( )

10

11 Wrapper Class Intro Sometimes you want to use fundamental variable type values, but the situation requires an object. There are classes that are just like the fundamental types except they are objects You have had experience with them a little when you used Integer and Double.

12 Wrapper Class cont. Integer intOne = new Integer(5);
What would be the value of: intOne == 5; ? How about intOne.compareTo(5); ? Integer class contains the methods: intValue( ) which returns the value in primitive form, compareTo(Object obj) which compares the Integer object to obj, equals(Object obj) which gives a boolean response to comparing the Integer object to obj, and toString( ) which is self-explanatory. Double has similar methods.

13 Array Lists ArrayList is located in java.util and must be imported.
ArrayList has the advantage of shrinking and growing in number of elements. ArrayList, however, can only contain Objects. (So you would have to use wrapper classes instead of int and Double).

14 ArrayList Methods ArrayList has an empty constructor
int size( ) returns the number of elements boolean add(Object obj) adds obj to end of list Object get(int index) returns element at index Object set(int index, Object obj) puts obj at index, returns what used to be at index. Void add(int index, Object obj) inserts obj at index, increases size by one. Object remove(int index) removes and returns object at index, decreases list size by one.

15 Using ArrayList What is wrong with the following: ?
ArrayList listOne = new ArrayList( ); listOne.add(5); Integer intObjTwo = new Integer(5); ArrayList listTwo = new ArrayList( ); listTwo.add(0, intObjTwo); integer intOne = listTwo.get(0);


Download ppt "Grouped Data Arrays, and Array Lists."

Similar presentations


Ads by Google