0% found this document useful (0 votes)
4 views15 pages

Arry

The document discusses arrays in Java, including what arrays are, why they should be used, and how to declare, initialize, and access elements of arrays of primitive data types. Example code is provided to demonstrate array processing in a program for rainfall statistics.

Uploaded by

kaung71669
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Arry

The document discusses arrays in Java, including what arrays are, why they should be used, and how to declare, initialize, and access elements of arrays of primitive data types. Example code is provided to demonstrate array processing in a program for rainfall statistics.

Uploaded by

kaung71669
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Java Programming: Array in Java

Dr. Kyawt Kyawt Htay


Lecturer

©https://blogs.ashrithgn.com/
Objectives

• To know what is array in java.

• To explain why we should use an array.

• To understand using an array of primitive data types in writing a program.

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Array in Java

• An array is a special type of container object which contains indexed collection of data
values of the same type.

• Arrays are created during runtime on the heap and follows the principle of the dynamic
allocation of data.

• The length of the array in Java cannot be increased or decreased after the array has
been created.

• Array length is fixed.

• Java supports arrays of all primitive and reference types.


Faculty of Computer Science ©https://blogs.ashrithgn.com/
Why we should use array?

• Consider a situation where we need to store five integer numbers. If we use


programming's simple variable and data type concepts, then we need five
variables of int data type and the program will be as follows −

int number1 = 10; int [ ] numarray = {10, 20, 30, 40, 50};
int number2 = 20;
int number3 = 30;
int number4 = 40;
int number5 = 50; Multiple values of the
same types store under
a single name.
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Array Declaration & Creation

• Array Declaration:
• The square brackets indicate the array declaration.
<data type> [ ] <arrayname> //Type-1 (Preferred)
(OR)
<data type> <arrayname> [ ] // Type-2 (C-style declaration)
• Array Creation:
<arrayname> = new <data type> [<size>]

• Example:
double [ ] rainfall = new double [12];

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Fixed-size and Variable-size Array

• Fixed-size array:
double [ ] rainfall = new double[12];

• Variable-size array:
int size;
int [ ] number; //declare an array
System.out.print(“Enter size of an array: ");
size = scanner.nextInt();
number = new int[size]; //create an array
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Array Initialization

• Like other data types, it is possible to declare and initialize an array at the same
time.
int [ ] number = { 1, 3, 5, 7 };

double [ ] samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2, 9.00,


No need to 3.123, 22.084, 18.08 };
specify size
String [ ] monthName = { “January”, “February”, “Much”, “April”, “May”,
“June”, “July”, “August”, “September”,
“October”, “November”, “December” };

• An array has a public constant length variable for the size of an array.
number.length 4

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Accessing Elements

• Individual elements in an array accessed with indexed expression.


index
rainfall
0 1 2 3 4 5 6 7 8 9 10 11

rainfall [2] Accessing element at


position #2

• The index of the first position in an array is 0.

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Array Processing : Practice Program
import java.util.Scanner;
class Ch10RainfallStat {
public static void main (String [ ] args) {
Scanner input = new Scanner(System.in);

String [ ] monthName = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };
double [ ] rainfall = new double[12];
double [ ] quarterAverage = new double[4];

double annualAverage, sum, oddMonthSum, oddMonthAverage, evenMonthSum, evenMonthAverage;


sum = 0.0;

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Cont’d

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


System.out.print("Rainfall for " + monthName[i] + ": ");
rainfall[i] = input.nextDouble();
sum += rainfall[i];
}
annualAverage = sum / 12.0;
System.out.printf( "\n Annual Average Rainfall: %06.2f\n\n", annualAverage);

monthName
0 1 2 3 4 5 6 7 8 9 10 11
January February March April May June July August September October November December

monthName[0] monthName[2] monthName[8]


Faculty of Computer Science ©https://blogs.ashrithgn.com/
Cont’d

oddMonthSum = evenMonthSum = 0.0;


for (int i = 0; i < rainfall.length; i += 2){
oddMonthSum += rainfall[i];
evenMonthSum += rainfall[i+1];
}
oddMonthAverage = oddMonthSum / 6.0;
evenMonthAverage = evenMonthSum / 6.0;

System.out.println("Odd & Even Month Rainfall Averages:");


System.out.printf("Odd Month Average: %-12.2f inches\n", oddMonthAverage);
System.out.printf("Even Month Average: %012.2f inches\n\n", evenMonthAverage);

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Cont’d

System.out.println("Quarterly Rainfall Averages:");


for (int i = 0; i < 4; i++) {
sum = 0;
for (int j = 0; j < 3; j++){
sum += rainfall[3*i + j]; //one quarter
}
quarterAverage[i] = sum / 3.0; //average for Quarter i+1
System.out.printf( "Average Qtr %d: %6.5f inches\n",
(i+1), quarterAverage[i]);
}
}
}
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Practice program

• Ch10RainfallStat.java

Faculty of Computer Science ©https://blogs.ashrithgn.com/


Summary

• What is array in java?

• Why we should use an array?

• How to use array of primitive data type arrays with sample program.

Faculty of Computer Science ©https://blogs.ashrithgn.com/


What will be next?

• Using Arrays of Objects

Faculty of Computer Science ©https://blogs.ashrithgn.com/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy