IS 12308-Object Oriented Programming: Lecture 8 - Arrays
IS 12308-Object Oriented Programming: Lecture 8 - Arrays
Programming
Lecture 8 - Arrays
By
Upeksha Kudagamage
Lecturer-SUSL
Array
Array is a data structures
Consisting of related data items of the same type.
Fixed length entity.
0 1 2 3 4
n-1
5-1=4
Index
2
Java Array Declaring
Primitive Array
Datatype[ ] variableName;
Datatype variableName[ ];
Ex :
int[ ] numbers;
int numbers[ ];
Ex :
1. int[ ] marks;
marks = new int[5];
Ex :
1. int[ ][ ] marks = new int[5][2];
2. int[ ][ ] marks = new int[5][ ];
5
Initialization an Array
6
Java Array Definitions
}
}
Note: print other values
7
Java Array Definitions
Ex ;
int[ ] numbers=new int[4];
numbers[0]=3;
numbers[1]=5;
numbers[2]=8;
numbers[3]=6;
8
The Array We Always Define
Class className{
public static void main(String args[]){
}
}
9
Example
Type a code for enter your Name Address And gender Using the
default Array.
Class ArgumentArray
{
public static void main(String args[])
{
System.out.print("Your Age :"+args[0]);
System.out.print("Your Address :"+args[1]);
System.out.print("Your Gender :"+args[2]);
}
10 }
How to Run the Program?
11
Example
Write a program to store 1 to 55 numbers in an integer
array and display those numbers.
Hint :
number is 1
number is 2
number is 3
-----------------
-----------------
number is 54
number is 55
12
class For_array{
public static void main(String args[]){
int marks[]=new int[55];
for(int i=0;i<marks.length;i++)
{
marks[i]=i+1;
System.out.println(" number is\t "+ marks[i]);
}
}
}
13
Example
Create a program to display following chart by using two
dimensional array.
Hint: 0 0 0 0 0
11111
22222
33333
44444
55555
66666
77777
88888
99999
14
class Array_dimensional{
public static void main(String[] args) {
15
Sorting Arrays
import java.util.Arrays;
class Array_sort{
public static void main(String[] args) {
int[] arrayNums;
arrayNums= new int[6];
arrayNums[0]=23;
arrayNums[1]=24;
arrayNums[2]=36;
arrayNums[3]=52;
arrayNums[4]=14;
arrayNums[5]=60;
Arrays.sort(arrayNums);
for (int i=0; i<arrayNums.length; i++) {
System.out.println(" number" + arrayNums[i]);
}
}
16 }
Arrays & Strings
class Array_string{
public static void main(String[] args) {
String[ ] aryString = new String[5] ;
aryString[0] = "This";
aryString[1] = "is";
aryString[2] = "a";
aryString[3] = "string";
aryString[4] = "array";
Arrays.sort(aryString);
for (int i=0; i<aryString.length; i++) {
System.out.println( aryString[i]);
}
}
18 }
Class ArrayEx01{
public static void main(String args[]){
Example int[] marks=new int[10];
Create a program for String[] stNames=new String[10];
System.out.print(stNames[0]+"\t"+marks[0]+"\n");
System.out.print(stNames[1]+"\t"+marks[1]+"\n");
System.out.print(stNames[2]+"\t"+marks[2]+"\n");
----------------------------------
}
}
19
Example
Set up an array to hold the following values, and in
this order: 23, 6, 47, 35, 2, 14. Write a program to get
the average of all 6 numbers.
20
class Answer_two{
public static void main(String[] args) {
int[ ] aryNums = { 24, 6, 47, 35, 2, 14 };
int arrayTotal = 0;
int average = 0;
22