L5-Array and Exceptions1

L5-Array and Exceptions1 - CSE205 Object-Oriented...

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

View Full Document Right Arrow Icon
CSE205 Object-Oriented Programming and Data Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
1-2 Outline ArrayList class ArrayList Class Exceptions & I/O streams - introduction
Background image of page 2
Arrays An array is an ordered list of values 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 scores The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9 What is scores[2]?
Background image of page 3

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

View Full DocumentRight Arrow Icon
Arrays For example, an array element can be assigned a value, printed, or used in a calculation scores[2] = 89; scores[first] = scores[first] + 2; mean = (scores[0] + scores[1])/2; System.out.println ("Top = " + scores[5]);
Background image of page 4
Arrays Another way to depict the scores array scores 79 87 94 82 67 98 87 81 74 91
Background image of page 5

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

View Full DocumentRight Arrow Icon
Declaring Arrays The scores array could be declared as follows int[] scores = new int[10]; The type of the variable scores is int[] (an array of integers) Note that the array type does not specify its size, but each object of that type has a specific size The reference variable scores is set to a new array object that can hold 10 integers
Background image of page 6
Examples Some other examples of array declarations float[] prices = new float[500]; boolean[] flags; flags = new boolean[20]; char[] codes = new char[1750];
Background image of page 7

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

View Full DocumentRight Arrow Icon
Using Arrays The iterator version of the for loop can be used when processing array elements for (int myArray : scores) System.out.println (myArray); This is only appropriate when processing all array elements from top (lowest index) to bottom (highest index)
Background image of page 8
Two-Dimensional Arrays A one-dimensional array stores a list of elements A two-dimensional array can be thought of as a table of elements, with rows and columns one dimension two dimensions
Background image of page 9

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

View Full DocumentRight Arrow Icon
Two-Dimensional Arrays To be precise, in Java a two-dimensional array is an array of arrays A two-dimensional array is declared by specifying the size of each dimension separately int[][] scores = new int[12][50]; A array element is referenced using two index values value = scores[3][6] The array stored in one row can be specified using one index
Background image of page 10
Two-Dimensional Arrays-Examples The following instantiates a two dimensional array of integers: int[][] seating = new int[5][10]; We can also instantiate and initialize the array as: int[][] seating = { {1, 2, 3}, {4, 5, 6}, {2, 2, 1} };
Background image of page 11

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

View Full DocumentRight Arrow Icon
ArryList class The ArrayList class is defined in java.util package. One advantage of ArrayList objects over arrays is to be able to store as many objects as we want. Arrays are fixed size, cannot store more data items than its size. ArrayList objects does not have a fixed size Also, we can store objects of different classes in an arraylist object. By using "add" method, we can store a Student object and a String object in an ArrayList object: ArrayList list1 = new ArrayList(); Student student1 = new Student(); String string1 = new String("abc"); list1.add(student1); // student1 is added to list1 list1.add(string1); // string1 is added to list1 too In this case, student1 object is stored at the index 0 and the string1 is stored at the index 1.
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

L5-Array and Exceptions1 - CSE205 Object-Oriented...

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

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