Arrays, Strings and Collections
Arrays, Strings and Collections
Collections [1]
Rajkumar Buyya
Grid Computing and Distributed Systems (GRIDS)
Laboratory
Dept. of Computer Science and Software Engineering
University of Melbourne, Australia
http://www.buyya.com
1
Arrays - Introduction
Arrays - Introduction
index
69
61
70
89
23
10
values
Declaration of Arrays
Declaration of Arrays:
Form 1:
Type arrayname[]
Form 2:
Type [] arrayname;
Examples:
int[] students;
int students[];
Note: we dont specify the size of arrays in the declaration.
Creation of Arrays
Examples:
Initialisation of Arrays
Example:
students[0] = 50;
students[1] = 40;
Arrays Length
// x = 7
// x = 40
7
Arrays Example
// StudentArray.java: store integers in arrays and access
public class StudentArray{
public static void main(String[] args) {
int[] students;
students = new int[7];
System.out.println("Array Length = " + students.length);
for ( int i=0; i < students.length; i++)
students[i] = 2*i;
System.out.println("Values Stored in Array:");
for ( int i=0; i < students.length; i++)
System.out.println(students[i]);
}
}
:w
Arrays Initializing at
Declaration
Example:
int[] students = {55, 69, 70, 30, 80};
Arrays Example
// StudentArray.java: store integers in arrays and access
public class StudentArray{
public static void main(String[] args) {
10
Two dimensional
arrays allows us to
store data that are
recorded in table.
For example:
Table contains 12
items, we can think
of this as a matrix
consisting of 4 rows
and 3 columns.
Item1
Item2
Item3
Salesgirl
#1
10
15
30
Salesgirl
#2
14
30
33
Salesgirl
#3
200
32
Salesgirl
#4
10
200
Sold
Person
11
2D arrays manipulations
Declaration:
Creation:
Initialisation:
Single Value;
myArray[0][0] = 10;
Multiple values:
13
14
Arrays of Objects
Arrays of Objects
16
17
Introduction
Example:
char place[] = new char[4];
place[0] = J;
place[1] = a;
place[2] = v;
place[3] = a;
19
String Class
Constructors
Utility
Comparisons
Conversions
Strings Basics
String stringName;
stringName = new String (string value);
Example:
String city;
city = new String (Bangalore);
Strings Arrays
Constructs an empty
String.
23
public int
Compare the Strings.
compareTo( String
anotherString)
public int
compareToIgnoreCase( Stri
ng anotherString)
reigonMatch(int start,
String other, int ostart, int
public trim()
public String
toLowerCase()
public String
toUpperCase()
Changes as specified.
25
26
StringDemo Output
String Length = 15
Modified String = Have a Nice Day
Converted to Uppercase = HAVE A NICE DAY
Converted to Lowercase = have a nice day
Summary