0% found this document useful (0 votes)
28 views

IS 12308-Object Oriented Programming: Lecture 8 - Arrays

This document discusses arrays in Java. It defines an array as a data structure consisting of related data items of the same type arranged in a fixed length. The document covers declaring and constructing both single and multi-dimensional arrays in Java. It also discusses initializing arrays, printing array elements, and provides examples of sorting and manipulating string and integer arrays.

Uploaded by

kasun Herath
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

IS 12308-Object Oriented Programming: Lecture 8 - Arrays

This document discusses arrays in Java. It defines an array as a data structure consisting of related data items of the same type arranged in a fixed length. The document covers declaring and constructing both single and multi-dimensional arrays in Java. It also discusses initializing arrays, printing array elements, and provides examples of sorting and manipulating string and integer arrays.

Uploaded by

kasun Herath
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

IS 12308-Object Oriented

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

Object Reference Array


Ex :
3 Thread t[ ];
Constructing an Array
Datatype[] variableName=new
Datatype[Size];
Array definition Array construction

Ex :
1. int[ ] marks;
marks = new int[5];

4 2. int[ ] marks = new int[5];


Constructing Multi-dimensional Array

Multi-dimensional array means array of array.

Ex :
1. int[ ][ ] marks = new int[5][2];
2. int[ ][ ] marks = new int[5][ ];

* You must have to specify at least the 1st bracket.

5
Initialization an Array

int[ ][ ] marks = new int[3][2];


Marks[0][1] = 30;

Declaring, constructing and initializing an array in one


line.
int[ ][ ] marks = { {2,3} , {5,6} ,{8,9} } ;

6
Java Array Definitions

Print an array element


Class ArrayT1{
Public static void main(String[] args){
int[ ] numbers={3,5,8,6};
Index 1
Index 0
System.out.println(numbers[0]);

}
}
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[]){
}
}

It is always String type

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?

Class Name Arguments

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) {

int[][] a2 = new int[10][5];

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


for (int j=0; j<5; j++) {
a2[i][j] = i;
System.out.print(" " + a2[i][j]);
}
System.out.println("\n");
}
}
}

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

for (int i=0; i < aryString.length; i++ ) {


System.out.print( aryString[i] + “ ” );
}
}
17
}
import java.util.Arrays;
Example class Array_string_sort{
public static void main(String[] args) {
 Above code is
translate to sorting
string array. 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];

store marks and the stNames[0]="sunil";


students names of 10 marks[0]=69;
students in an array and stNames[1]="Nimal";
retrieve it. marks[1]=80;
stNames[2]="Aruna";
marks[2]=39;
----------------------------------
System.out.print("Name \t Marks \n.............\n");

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;

for (int i=0; i < aryNums.length; i++) {


arrayTotal = arrayTotal + aryNums[ i ];
}

average = arrayTotal / aryNums.length;


System.out.println("total: " + average);
}
21 }
END

22

You might also like