07-arrays - Arrays and ArrayLists CSE 114: Computer Science...

Info iconThis preview shows pages 1–14. 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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight 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 CSE 114: Computer Science I Arrays Programs often operate on large quantities of similar data Assigning a unique variable (and name) to each piece of data is tedious Ex. var1, var2, var3, ... An array is a collection of many variables of the same type, all under one name Arrays can be of any type Array Storage a[0] a[1] a[2] a[3] a[4] a[5] int a = new int[6]; Declaring An Array To declare an array, follow the type with square brackets: double foo; To create an array, use new and a size: foo = new double[5]; Array sizes must be non-negative integer values! Array Size The size of an array is Fxed upon creation The length Feld returns an arrays size int a = new int[5]; int size = a.length; // size is 5 Note that length has no parentheses! Defnitions char c; int value = new int[10]; Causes Array object variable c is un-initialized Array object variable v reFerences a new ten- element list oF integers Each oF the integers is deFault initialized to 0 Examples Array Examples char alphabet = new char[26]; int numPlayers = 5; int scores = new int[numPlayers]; String phrases = new String[15]; Creating Arrays Arrays can also be initialized when declared: int bar = {5, 4, 3, 2, 1}; The size of the array is determined by the size of the initialization list In this example, bar has Fve elements Array Elements Individual elements of an array are accessed by using the array name, followed by an (integer) index value, enclosed in brackets Ex. myArray[1] Indices are numbered starting with 0 Thus, myArray[1] refers to the second element in myArray Consider int v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); Consider int v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); Consider int v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); Consider...
View Full Document

This note was uploaded on 04/07/2008 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.

Page1 / 48

07-arrays - Arrays and ArrayLists CSE 114: Computer Science...

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

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