{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L5-Array and Exceptions1

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

Info icon This 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
Image of page 1

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

View Full Document Right Arrow Icon
1-2 Outline ArrayList class ArrayList Class Exceptions & I/O streams - introduction
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]?
Image of page 3

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

View Full Document Right 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]);
Image of page 4
Arrays Another way to depict the scores array scores 79 87 94 82 67 98 87 81 74 91
Image of page 5

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

View Full Document Right 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
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];
Image of page 7

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

View Full Document Right 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)
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
Image of page 9

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

View Full Document Right 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
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} };
Image of page 11

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

View Full Document Right 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.
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern