05_arrays (1)

05_arrays (1) - Java 211 Yosef Mendelsohn ARRAYS Often, we...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Java 211 – Yosef Mendelsohn ARRAYS Often, we may want to organize a group of data together. For example, supposing we wish to gather 100 scores on a final exam together. We could declare 100 different variables each with their own name, but this is hardly an elegant solution. Instead, we can use a construct called an ‘array’ which allows us to group multiple values (of any data type) under a single variable name. For example, to declare an array of 100 exam scores together, I could do the following: //declaring an array of 100 integers int[] scores; //creating an array reference scores = new int[100]; //creating the array object Note: In Java, arrays are objects . In other words, the identifier ‘scores’ is NOT a primitive data type of int. Instead, it is a an object that holds a group (array) of integers. This concept will become increasingly important – and useful – as we become more advanced in our use of arrays. The array called ‘scores’ defined earlier can now hold 100 integer values. To insert values into the scores array I could type the following scores[0] = 89; scores[1] = 93; scores[2] = 71;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
scores[99] = 83; Notice how the first element of the array is called scores[0] (spoken as “scores sub zero”), and the last element of the array is called scores[99]. The point is that arrays range from 0 to the declared size of the array minus one . So an array of declared size 85 has indexes ranging from 0 to 84. Recall also that arrays are objects. This is why we needed to use the ‘new’ operator to create the array. So an array is simply a list of values grouped under one identifier . All the elements of the array must be of the same data type. Arrays can store any type of data: You can have an array of ANY type you want. Eg: An array of int, double, char, String, etc. You can even have an array of arrays (of arrays). Okay, that last one’s just mean… but it is perfectly legal! double[] gymnasticScores = new double[10]; char[] letterGrades = new char[25]; String[] studentNames = new String[25]; Arrays and loops: Because arrays are lists of values, we frequently want to iterate through the list. For example, we may want to output every value in the array to the screen. Or we may wish to check to see if a certain value is present in the array. Or we may wish to check how many times a certain value is present in the array. The possibilities are endless.
Background image of page 2
For this reason, arrays go hand-in-hand with loops. For example, to output all elements of the scores array described above, we could use a loop such as this one: for (int j=0; j<100; j++) System.out.println( scores[j] ); This type of behavior, where you iterate through an entire array doing things such as
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

05_arrays (1) - Java 211 Yosef Mendelsohn ARRAYS Often, we...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online