19
Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal [email protected]

Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal [email protected]

Embed Size (px)

Citation preview

Page 1: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Object Oriented Programming

Lecture 5: Arrays and Strings

Mustafa Emre İ[email protected]

Page 2: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Recap

• Assignment 4

• Flow Control– Conditions / Decisions

– Loops

Page 3: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Today

• Assignment 04• Arrays – Strings

• Thinking in Java – Chapter 4,5

Page 4: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Arrays

• Need for an Array [group]– To group elements of a particular type

– To apply a procedure to individual elements within a larger group

• Example: assume we have 50 Circles– We do not want to create 50 different variables.

– What would the code to find the sum of their areas look like?

– Why 50? Why any pre-determined number? Why not let the user determine the number?

Page 5: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Java Arrays

• The Array is an ordered group data elements of a single data type (primitive or reference).

• Java arrays are objects (reference type)• Arrays can be declared and initialized seperately

– Declaration is by specifying the data type – Initialization requires the number of elements to be

stored.• int[] intArray; //square brackets declare the array• intArray = new int[10]; // memory for 10 int’s are

allocated and intArray points to the array

note the “new” statement• int intArray[] = new int[10] // alternative –

same result

Page 6: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Index

• The value within the square brackets define the order of the element of interest within the array. (Its index)

• Java arrays start counting the index at 0. (not 1)

• intArray[2] = 7; // the third element in the array is 7

Page 7: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

.length

• Java arrays are objects. – Slightly slower.– Lots of convenience

• Each array has a variable “.length“. This variable remembers the storage capacity the array.

• Very convenient in setting up loopsint index = 0;while ( index < intArray.length ) {

System.out.println (intArray[index++]); }

Page 8: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Initialization of Elements

• Nothing to be done for primitives• Object arrays are reference types

– A new object needs to be initialized at the reference point.

Circle[] circleArray = new Circle[10];

for (int j=0; j<10; j++) {

circleArray[j] = new Circle();

}

Page 9: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Multi Dimensional Arrays

• Arrays do not have to represent a one dimensional relationship (queues) between elements.

• Multi-dimensional structures (tables, time-series) can also be abstracted– Note that humans have difficulty in imagining beyond

three dimensionsCell[][] spreadsheet = new Cell[25][50];

spreadsheet[row][column];

classroom[][][] university= new classroom[10][8][4];

university[building][floor][room]

Page 10: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Pre-known Data

• Alternative way to initialize an array involves providing a listing of all the data to be stored.int[] fibonacci = {0,1,1,2,3,5,8,13};

• For multi-dimensional arrays:int[][] matrix= { {0,1,2},{1,0,1},{34,23,6} };

Page 11: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

java.lang.Vector• Array size is fixed once the array is initialized.• When more data needs to be added than the size allows, a

new array of a larger size needs to be initialized and the elements of the old array copied into the new one.

• Vector Class is a data structure that has no size limitation. Vectors are utilized through the methods the class provides. Some methods:

public Vector(int initialSize);public Vector(); // create empty vectorpublic void addElement(Object obj);public Object elementAt(int index);public void insertElementAt(int index);public void removeElementAt(int index);public int indexOf(Object obj);

Page 12: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Character Arrays (String)

• Text is an array of characters (string)• Java provides the String class for convenient

manipulation of these character arrays.• Using the String class is almost as easy as using

primitive data types.

Page 13: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

String

• Points to watch out for– The content cannot be subjected to comparison by the

“==“ operator.

the .equals() method needs to be used.firstString.equals(String secondString)

– String object does not allow altering the content directly. Useful to use helper classes

Page 14: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

StringBuffer

• String class is not designed to modify the character contents (value) but to manage the container

• StringBuffer class should be used to add to or strip characters from the array.

Page 15: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

StringBuffer methods

• Some examples:– public int charAt(int index)– public StringBuffer append(“anytype” t)– public StringBuffer insert(int offset, “anytype” t)– public deleteCharAt(int index)– public delete(int start, int end)– public StringBuffer subString(int start, int end)– public String reverse()– …

Page 16: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

preparation - chapter 4,5

• Chapter 4– Constructor

– Overloading– this() for calling one constructor from others

– static– finalize() the method that is called before the object is deleted

• Chapter 5– package *

– public – private – protected – "friendly"

Page 17: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Assignment 05

• Add the following to the Circle class– Variable for name– Variable for color

• Substantial changes in CircleApplication– Create 100 circles

• Each should have a name in the form of “Circle-XXX" according to its order of creation.

• Use Math.random() to assign the following properties– Color: Either Red , Green, Blue, Yellow, Puple, Cyan, Orange,

Brown, Black, or White (use an array for color list)– Coordinates for the center and the radius

Page 18: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Assignment 05

– Reports to be displayed:• List the circles in alphabetical order, displaying the

properties of each.

• Sum of all areas and the average of all areas

• The number of circles for each color

• List of red circles sorted in order of size.

• The farthest and closest circles (in terms of their centers) to a randomly determined point.

Page 19: Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal emreilal@iyte.edu.tr

Next week

• Preparation: Thinking in Java Chapter 6