Tcs 2044 Java Programming Chapter 3 Array and String (Week 6)
Tcs 2044 Java Programming Chapter 3 Array and String (Week 6)
Objectives
1. Understand the concept of arrays 2. Learn the steps involved in using arrays - declaring, creating, initialising and processing arrays. 3. Use objects as array elements. 4. Use multidimensional arrays. 5. Recognise the difference between arrays and strings. 6. Use and manipulate strings.
or
datatype arrayName[];
Creating an Array
Because an array is object, the declaration does not allocate any space in memory for the array. q Elements cannot be assigned before array is created. q new operator can be used to create the array. q Syntax to create array
q
Combine declaration and q creation Syntax for declaring and creating an array in one statement.
datatype[] arrayName = new datatype[arraysize];
or
datatype arrayName[] = new datatype[arraysize];
Example
double[] myList = new double[10];
The array myList has 10 elements of double type and integer indices from 0 to 9.
When arrays are created, the elements are assigned the default value as follow: Default value Types of variables 0 \u0000 false null Numeric primitive datatype char boolean object
The array elements are accessed through the index (must start from 0).
6
Arrays of Objects
Declaring an Array of Objects
q Syntax
or
classname arrayobjectName[] = new classname[arraysize];
Copying arrays
Using for loop
q
program
10
Using (=) operator does not copy the content of the array newList = list;
Changes the value of newList with list and whatever changes happen in array newList will affect the list array.
11
12
public static void printList(String s, int[] list) {System.out.print(s + " " ); for (int i= list.length;i+) 0;i< + System.out.print(list[i] + " " ); Output of the program System.out.print(" ); \n" } }
13
Multidimensional Array
Usually used to represent a matrix or a table. q Declare and initialize two dimensional array q Two subscript are used, one for the main array and the other one for the array contained within the main array.
q
Example
int[][] matrix = new int[5][5];
or
int matrix[][] = new int[5][5];
14
Example
public class Test2D_Array{ public static void main(String [] args){ int [][]x={{10,20,30,40},{40,30,20,10},{10,20,30,40}}; int total=0; for(int i=0;i<3;i++){ for(int j=0;j<4;j++){ total= total + x[i][j]; } } System.out.println(Total: +total); } }
X[0][0] X[1][0] X[2][0]
Memory representation
X[0][0] X[0][1] X[0][2] X[0][3]
10 40 10
20 30 20
30 20 30
40 10 40
15
String
String class
q q q
The java.lang.String class represents character strings. Can create a string with class String using keyword new Syntax to create a string : String newString = new String(s); or String newString = String constant;
Example String message = new String Welcome To Java; String message = Welcome To Java;
MANAGEMENT & SCIENCE UNIVERSITY
16
String comparisons
q
Method equals() can be used for the equality comparison of the contents of objects. (string1.equals(string2) )
17
Example Program of Using == operator, equals() and compareTo() method to compare string
class TestCom pareString { public static void m ain(String arg[]) { String string1 = " M anagem & Science University ent "; String string2 = "M anagem & Science ent Univer sity"; if (string1.equals(string2)) System .out.println("string1 and string2 have else System .out.println("string1 and string2 if (string1.com pareTo(strin g2) == 0) System .out.println("string1 and string2 are else System .out.println("st ring1 and string2 } } the sam contents"); e are not equal"); the sam "); e are different ");
string1 and string2 have the same contents string1 and string2 are the same
MANAGEMENT & SCIENCE UNIVERSITY
18
String concatenation
Combines two strings using + operator. q Extracts substring from a string using substring() method
q
String length
Gets the length of a string. q Uses length() method
q
Splitting string
Strings can be separated according to characters q Uses split() method
q
19
Example program to concatenate string and using substring() and length() methods
class TestString{ public static void main(String arg[]) { String string1 = " Welcome to MSU "; String string2 = "Management & Science University "; String string3 = string1 + string2; String string4 = string1.substring( 0,11) + string2; System.out.println("string3 : " +string3); System.out.println("string4 : " +string4); System.out.println("Length of string 3: " + string3.length()); System.out.println("Length of string 4: " + string4.length()); } }
MANAGEMENT & SCIENCE UNIVERSITY
20
Output:
string3: Welcome to MSU Management & Science University string4: Welcome to Management & Science University Length of string3: 46 Length of string4: 42
21
22