0% found this document useful (0 votes)
95 views22 pages

Tcs 2044 Java Programming Chapter 3 Array and String (Week 6)

This document discusses arrays and strings in Java. It covers declaring, initializing, and processing arrays, including multidimensional arrays. It also discusses the String class and methods for comparing, concatenating, extracting substrings from, and splitting strings. Key topics include using for loops and arraycopy() to copy arrays, arrays of objects, default array values, and representing 2D arrays as matrices in memory.

Uploaded by

Nazfar Abdullah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
95 views22 pages

Tcs 2044 Java Programming Chapter 3 Array and String (Week 6)

This document discusses arrays and strings in Java. It covers declaring, initializing, and processing arrays, including multidimensional arrays. It also discusses the String class and methods for comparing, concatenating, extracting substrings from, and splitting strings. Key topics include using for loops and arraycopy() to copy arrays, arrays of objects, default array values, and representing 2D arrays as matrices in memory.

Uploaded by

Nazfar Abdullah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 22

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

CHAPTER 3 ARRAY AND STRING

MANAGEMENT & SCIENCE UNIVERSITY

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.

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Declaring Array Creating Array


Declaring an Array
Syntax :
datatype[] arrayName;

or
datatype arrayName[];

Example double[] myList; or double myList[];


MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

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

arrayName = new datatype[arraysize];

Example MyList = new double[10];


MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

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];

myList[0] myList[1] myList[2] myList[3] myList[4] myList[5]

The array myList has 10 elements of double type and integer indices from 0 to 9.

myList[6] myList[7] myList[8] myList[9]

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Initialising Array Processing Array


q

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

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Arrays of Objects
Declaring an Array of Objects
q Syntax

for declaring an array of objects

classname[] arrayobjectName = new classname[arraysize]

or
classname arrayobjectName[] = new classname[arraysize];

Example Box[] boxArray = new Box[10];

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Copying arrays
Using for loop
q

Copying individuals elements. Example

for (int i =0; i< sourceArrays.length;i++){ targetArray[i] = sourceArray[i]; }

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Using arraycopy() method


q

Syntax to use arraycopy()


arraycopy(sourceArray,src_pos,targetArray,tar-pos, length)

MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Example of Using arraycopy() method


class TestCopyWitharrayCopy {public static void main(String arg[]) { sourceArray =1 00,5 0,2 0,800} int[] {00,2 5 3 ; int[] targetArray = new int[sourceArray.length]; System.out.print(" \nSourceArray :" ); printArray(sourceArray); System.out.print(" \nTarget Array :" ); printArray(targetArray); System.arraycopy(sourceArray,0,targetArray,0,sourceArray.length); System.out.print(" \nSourceArray :" ); Output of the printArray(sourceArray); System.out.print(" \nTarget Array :" ); printArray(targetArray);} static void printArray(int[] a) {for(int i= a.length;i+) 0;i< + System.out.print(" "+ a[i]);} }

program

MANAGEMENT & SCIENCE UNIVERSITY

10

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Effect of using (=) operator


q

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.

MANAGEMENT & SCIENCE UNIVERSITY

11

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Example of using (=) operator to copy array


c s Te tC p A y la s s o y rra { u lics ticv idm in( tring p b ta o a S a [) rg] { ]lis =01 , , , } int[ t {, , 345; 2 int[ ne Lis ne ] w t= w int[lis le th] t. ng ; ne Lis = t; w t lis S s mo t. rintln (B fo m d inglis ) y te . u p " e re o ify t"; S s mo t. rintln (----------------------"; y te . u p " ) p tLis " t is :, t) rin t(Lis " ; lis p tLis " ne Lis is:, ne Lis ; rin t( w t " w t) fo (int i=; lis le th; + r 0i< t. ng i+ ) lis = ; t[i] 0 S s mo t. rintln (\nA r m d inglis ) y te . u p " fte o ify t"; S s mo t. rintln(----------------------"; y te . u p " ) p tLis " t is :, t) rin t(Lis " ; lis p rintLis " w t is:,ne Lis ; t(ne Lis " w t) }
MANAGEMENT & SCIENCE UNIVERSITY

12

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

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" } }

MANAGEMENT & SCIENCE UNIVERSITY

13

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

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];

MANAGEMENT & SCIENCE UNIVERSITY

14

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

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

Output of the program Total: 300


MANAGEMENT & SCIENCE UNIVERSITY

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

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

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

String comparisons
q

Method equals() can be used for the equality comparison of the contents of objects. (string1.equals(string2) )

Method compareTo() is can be used to compare strings. (string1.compareTo(string2) )

MANAGEMENT & SCIENCE UNIVERSITY

17

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

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 ");

Output of the program

string1 and string2 have the same contents string1 and string2 are the same
MANAGEMENT & SCIENCE UNIVERSITY

18

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

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

MANAGEMENT & SCIENCE UNIVERSITY

19

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

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

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Output:
string3: Welcome to MSU Management & Science University string4: Welcome to Management & Science University Length of string3: 46 Length of string4: 42

MANAGEMENT & SCIENCE UNIVERSITY

21

TCS 2044 JAVA PROGRAMMING

CHAPTER 3 ARRAY AND STRING (Week 6)

Example using split() method


class TestSplitString{ public static void main(String [] args){ String s1= Welcome to Java Programming; String [] s2 = s1.split(//s); for(int i=0; i<s2.length; i++){ System.out.println(+s2[i]); } } }

Output: Welcome to Java Programming

MANAGEMENT & SCIENCE UNIVERSITY

22

You might also like