0% found this document useful (0 votes)
35 views24 pages

Array Variable: Java Programming (Object)

Array is a set of data with the same name and the same type. Array is one of the best way to store large of data. To use an array you must first declare the array variable to reference the array and specify the type of the array variable.

Uploaded by

Hunly Chheng
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views24 pages

Array Variable: Java Programming (Object)

Array is a set of data with the same name and the same type. Array is one of the best way to store large of data. To use an array you must first declare the array variable to reference the array and specify the type of the array variable.

Uploaded by

Hunly Chheng
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Array Variable

CHAPTER 4
J A V A P R OG R A MMI NG
( OB J E C T )
1
H O R S O N O E U N
WHAT IS AN ARRAY OBJECT?
- Array is a set of data with the same name and the
same type.
- The element of array is recognized by index of the
array.
- The index of array is usually started from the zero
number (0) enclosed in the square bracket ([ ]).
- Elements stored in sequence of index.
- Array is one of the best way to store large of data.
- To use an array you must first declare the array
variable to reference the array and specify the type
of the array variable.
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

2
ARRAY OBJECT
Declaration
| To declare an array, you have to think of:
y What is the type of data to be stored (Data Type)
y Name of the array
y Array Size (Elements to be stored in the array)
| Keyword new is needed since array is a kind of
Object Variable:
| Syntax:
DataTye[] ArrayName[] = new DataTye[Size];
DataTye ArrayName[]= new DataTye[Size];
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

3
ARRAY OBJECT
Declaration (Cont.)
| Example Declare an array to store 5 elements of
integer numbers.
int intNum[ ] = new int[5]; or
int[ ] intNum = new int[5];


| Example Declare an array to store 5 elements of
floating point number.
double dblNum[ ] = new double[5]; or
double[ ] dblNum = new double[5];

J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

4
intNum
null null null null Null
intNum[0] intNum[1] intNum[2] intNum[3] intNum[4]
dblNum
null null null null Null
dblNum[0] dblNum[1] dblNum[2] dblNum[3] dblNum[4]
ARRAY OBJECT
Declaration (Cont.)
| Example Declare an array to store 25 characters.
char ch[ ] = new char[25]; or
char[ ] ch = new char[55];


| Example Declare an array of String to store 4 names
characters.
String name[ ] = new char[4]; or
String[ ] name = new char[4];

J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

5
intNum
null null .. null Null
ch[0] ch[1] .. ch[23] ch[24]
Name
null null null null
name[0] name[1] name[2] name[3]
ARRAY OBJECT
Initializing an Array
| This is the way that you can set values to an array as
soon as the array declared.
| You can initialize array in two ways.
| Example with the five integer numbers:

J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

6
int [ ] intNum= {11, 2, 23, 84, 55}

Or You can write
int [ ] intNum= new int [5];
intNum[0]=11;
intNum[1]=2;
intNum[2]=23;
intNum[3]=84;
intNum[4]=55;
intNum
11 2 23 84 55
intNum[0] intNum[1] intNum[2] intNum[3] intNum[4]
ARRAY OBJECT
Accessing elements in an Array
| To work with the individual element of an array such
as
y Setting value
y Edit Value
y Delete the element
y Retrieving value
| Every thing depends on index of the array.
| Example you have a array of String with five people as
below:
String[] people = new String[5];

J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

7
Name
null null null null null
name[0] name[1] name[2] name[3] name[4]
ARRAY OBJECT
Accessing elements in an Array (Cont.)
|Then you can set the value to the array as
below:
people[0]= Sonoeun;
people[1]= Phanny;
people[2]= Veasna;
people[3]= Narith;
people[4]= Phanna;
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

8
Name
Sonoeun Phanny Veasna Narith Phanna
name[0] name[1] name[2] name[3] name[4]
ARRAY OBJECT
Accessing elements in an Array (Cont.)
| If you want to display the string Narith, you write the
code as below:
System,out.println(people[3]);

| If you want to edit the string Phanny to Nary, you
write the code as below:
people[1]=Nary

J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

9
Name
Sonoeun Nary Veasna Narith Phanna
name[0] name[1] name[2] name[3] name[4]
ARRAY OBJECT
See the Example1:
| Program D Array

Program

y


y
y
y j
y
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

10
ARRAY OBJECT
See the Example1: (Cont.)
| t Example 1
|

Functions
y


y
y
y j
y
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

11
Program
getData() DelData() SearchData() sortData() showData()
ARRAY OBJECT
See the Example1: (Cont.)
| j D D
y


y
y j j
y
y
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

12
ARRAY OBJECT
See the Example1: (Cont.)

J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

13

public class ArrEx1 {

//Write your functions here

public static void main(String[] args) {

//Block of main jobs

}
}
public static void getData()
{

}
public static void DelData()
{

}
public static void sortData()
{

}
public static void searchData()
{

}
public static void showData()
{

}
ARRAY OBJECT
See the Example1: (Cont.)
| Function







J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

14
public static void getData(int[] arrNum)
{
Scanner sc = new Scanner (System.in);
int i;
for (i=0; i<nMax; i++)
{
//System.out.print("Number [" +(i+1)+"] : " );
//arrNum[i]=sc.nextInt();
String str = "Number [" +(i+1)+"] : ";
arrNum[i]=Integer.parseInt(JOptionPane.showInputDialog(str, "00"));
}
}
ARRAY OBJECT
See the Example1: (Cont.)
| Function
| Function
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

15
public static void searchData(int[] arrNum, int n, int sVal)
{
int i, j, temp;
for (i=0; i<n; i++)
if (arrNum[i]==sVal)
System.out.println("Number [" +(i+1)+"] : "+arrNum[i]);
}
public static void showData(int[] arrNum)
{
int i;
for (i=0; i<nMax; i++)
System.out.println("Number [" +(i+1)+"] : "+arrNum[i]);
}
ARRAY OBJECT
See the Example1: (Cont.)
| Function j
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

16
public static void sortData(int[] arrNum, int n)
{
int i, j, temp;
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++)
if (arrNum[i]>arrNum[j])
{
temp = arrNum[i];
arrNum[i]=arrNum[j];
arrNum[j]=temp;
}
}
End Loop
i<nMax-1
True
F
a
l
s
e

i++
j<nMax
True
Swap Value
i=0
j=i+1
arrNum[i]>arrNum[j]
J++
False
False
True
ARRAY OBJECT
See the Example1: (Cont.)
| Function





J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

17
public static void delData(int[] arrNum)
{
int i, j, temp, dVal;
dVal=Integer.parseInt(JOptionPane.showInputDialog("Enter the
number you want to delete:","00"));
for (i=0; i<nMax; i++)
if (arrNum[i]==dVal)
{
for (j=i; j<nMax-1; j++)
{
temp = arrNum[j];
arrNum[j]=arrNum[j+1];
arrNum[j+1]=temp;
}
nMax=nMax-1;
}
}
ARRAY OBJECT
See the Example1: (Cont.)
| The main function Program





J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

18
public static void main(String[] args) {
int numArr [] = new int [nMax];
getData (numArr);
System.out.println("Showing Data");
showData (numArr); //Call showData Function.
System.out.println("Sorting Data");
sortData(numArr, nMax); //Call sortData Function.
System.out.println("Showing Data After Sort");
showData (numArr); //Call showData Function again.
System.out.println("Searching Data");
int sInt = Integer.parseInt(JOptionPane.showInputDialog("What number you want to search?","00"));
searchData (numArr, nMax, sInt); //Call searchData Function.
System.out.println("Deleting Data");
delData(numArr); //Call DelData Function.
System.out.println("Showing Data After Deleted");
showData (numArr); //Call showData Function again.
}
nMax is declare as global
PRACTICE/HOMEWORK
1. Program Array
j

DUser.
2. Program Array

DUser.
3. Program j
j j

DUser D
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

19
ARRAY OBJECT
Two Dimension Array
|Two dimension arrays are often used to
represent tables of values consisting of
information arranged in rows and columns.
|To identify a particular table element, we must
specify two subscripts.
y The first identifies the element's row
y and the second identifies the element's column.

Or
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

20
DataType ArrName[ ][ ] = new DataType [Row][Column];
DataType[ ][ ] ArrName = new DataType [Row][Column];
ARRAY OBJECT
Two Dimension Array
| Example you want to declare an array to store integer
numbers of 3 rows and five column. So you can do as
below:
int[][] num=new int[3][5];



| Then, you may want to set the value of 45 to third
element of the array you can write code as below:
num [0][2] = 45;

J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

21
num
num[0][0] num[0][1] num[0][2] num[0][3] num[0][4]
num[1][0] num[1][1] num[1][2] num[1][3] num[1][4]
num[2][0] num[2][1] num[2][2] num[2][3] num[2][4]
ARRAY OBJECT
Initializing Two Dimension Array
| To initialize a two dimension array, you use the brace
{ } separate each line/row of the elements.
| See the example below. It initializes the 15 elements
of floating point with 3 rows and 5 column.
float[ ][ ] num={{12.3,3.24,45,9.58,23.5},
{12.5,30,45.9,9.5,25.5}, {34,3.5,45.7,10,50.5}}


Then if you want to display the number of 45.9 you write:
System.out.println(num[1][2]
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

22
num
12.3 3.24 45 9.58 23.5
12.5 30 45.9 9.5 25.5
34 3.5 45.7 10 50.5
PRACTICE/HOMEWORK
1. Program User


Array Rows Column.
2. Program User


Array Rows Columns.
3. Program
Matrix
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

H
o
r

S
o
n
o
e
u
n

23
Matrix 1
Matrix 2 Matrix 3
1 3 5
7 9 11
2 4 6
12 5 8
3 7 11
19 14 19
End!
CHAPTER 4
J
a
v
a

P
r
o
g
r
a
m
m
i
n
g

(
O
b
j
e
c
t
)

24
H
o
r

S
o
n
o
e
u
n

You might also like