Arrays and ArrayLists

Arrays and ArrayLists - Arrays and ArrayLists Arrays of...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: Arrays and ArrayLists Arrays of objects Suppose that we want to have an array of objects of the Student class: Student students = new Student[100]; Before we use each object in this array, for each index, an object needs to be instantiated. as: students[0] = new Student(); students[1] = new Student(); and so on. Or we can use a for loop to instantiate all: for (int i=0; i < students.length; i++) students[i] = new Student(); Multi-dimensional Arrays 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} }; seating.length returns the size of first dimension and seating[0].length returns the size of second dimension. The ArrayList 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. In arrays, once we fix its size, we cannot change it and cannot store more data than its size. In case that we do not know how much data we will be storing, this is a problem. Also while we can store objects of only one class in an array, we can store objects of different classes in one...
View Full Document

This note was uploaded on 06/11/2009 for the course CS 205214 taught by Professor Balasooriya/kouvetakis during the Spring '09 term at ASU.

Page1 / 3

Arrays and ArrayLists - Arrays and ArrayLists Arrays of...

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

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