Presentation is loading. Please wait.

Presentation is loading. Please wait.

02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)

Similar presentations


Presentation on theme: "02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)"— Presentation transcript:

1 02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)

2 02/09/2005CSE 1101 public class Frog{ private boolean formerPrince; private String phrase1; private String phrase2; private String phrase3; private String phrase4; private String phrase5; private String phrase6; private String phrase7; private String phrase8; private String phrase9; private String phrase10;... What if our Frog (from homework 2) could say 10 different things?

3 02/09/2005CSE 1102 public class CrashCop{ private Taxi taxi1; private Taxi taxi2; private Taxi taxi3; private Taxi taxi4; private Taxi taxi5; private Taxi taxi6; private Taxi taxi7; private Taxi taxi8; private Taxi taxi9; private Taxi taxi10; private Taxi taxi11; private Taxi taxi12; private Taxi taxi13; private Taxi taxi14; private Taxi taxi15; What if our CrashCop (from homework 3) could watch 15 taxis?

4 02/09/2005CSE 1103 public class Sitter{ private Sitter kid1; private Sitter kid2; private Sitter kid3; private Sitter kid4; private Sitter kid5; private Sitter kid6; private Sitter kid7; private Sitter kid8; private Sitter kid9; pirvate Sitter kid10; private Sitter kid11; private Sitter kid12; private Sitter kid13; private Sitter kid14; private Sitter kid15; private Sitter kid16; private Sitter kid17; private Sitter kid18; private Sitter kid19; pirvate Sitter kid20; What if our Sitter (from homework 4) could watch 20 kids?

5 02/09/2005CSE 1104 public class IToonList{ private IToon toon1; private IToon toon2; private IToon toon3; private IToon toon4; private IToon toon5; private IToon toon6; private Ttoon toon7; private IToon toon8; private IToon toon9; private Ttoon toon10; private IToon toon11; private IToon toon12; private IToon toon13; private IToon toon14; private IToon toon15; private IToon toon16; private Ttoon toon17; private IToon toon18; private IToon toon19; private Ttoon toon20; private IToon toon21; private IToon toon22; private IToon toon23; private IToon toon24;... What if we want to store 500 Itoons (from homework 2)?

6 02/09/2005CSE 1105 What if we want to store lots of things… …but we don’t want to declare a separate variable for each one? That’s what arrays are good for.

7 02/09/2005CSE 1106 int[] data; data = new int[5]; data[0] = 6; data[1]= 10; data[2] = 12; What is an Array? It’s an easy way to declare lots of variables that all have the same type.

8 02/09/2005CSE 1107 Array Elements and Indices The “data” array has 5 elements: data[0] whose value is 6 data[1] whose value is 10 data[2] whose value is 12 data[3] whose value is 0 data[4] whose value is 0 When accessing an element, the number within square brackets is called an index The valid indices are 0 thru (array length - 1)

9 02/09/2005CSE 1108 int[] data; data is a reference variable whose type is int[], meaning “array of ints”. At this point its value is null. data = new int[5]; The new operator causes a chunk of memory big enough for 5 ints to be allocated on the heap. Here, data is a assigned a reference to the heap address. data[0] = 6; data[1]= 10; data[2] = 12; Initially, all five ints have the value 0. Here, three of them are assigned other values. int[] info = {6, 10, 12, 0, 0}; // another way An Array is an Object (with unique [] syntax)

10 02/09/2005CSE 1109 int x = data[0]; data[3] = data[2]; data[4] = sum(1,5); data[4] = sum(1, data[4]); System.out.println(“data[4] is “ + data[4]); x = data[0] * data[1] * data[2]; Person p = new Person; p.setAge(data[3]); Operations on Array Elements An “element” of an array of ints can be used virtually anywhere an expression of type int is valid. Likewise for arrays of other types.

11 02/09/2005CSE 11010 int[] data; data = new int[5]; data[0] = 6; data[1]= 10; data[2] = 12; int result = 0; // the value of data.length is 5 for (int i =0; i < data.length; i++){ result = result + data[i]; } An Array’s “length” Instance Variable

12 02/09/2005CSE 11011 double[] temps; temps = new double[24]; temp[0] = 78.5; temp[1] = 84.2; boolean[] answers = new boolean[6]; answers[0] = true; if (answers[0) foo(); char[] buffer = new char[500]; open a file for reading while (more chars in file & buffer not full) buffer[i++] = char from file Arrays of Other Primitive Types


Download ppt "02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)"

Similar presentations


Ads by Google