0% found this document useful (0 votes)
21 views3 pages

Contoh Program Array Java

The document contains examples of Java programs using arrays. It shows how to declare, initialize, access, and loop through single and multi-dimensional arrays. The programs demonstrate different ways to store, retrieve and manipulate data from arrays.

Uploaded by

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

Contoh Program Array Java

The document contains examples of Java programs using arrays. It shows how to declare, initialize, access, and loop through single and multi-dimensional arrays. The programs demonstrate different ways to store, retrieve and manipulate data from arrays.

Uploaded by

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

Contoh Program Array

Program 1
public class JavaExample{
public static void main(String args[]){
//array declaration
String names[] = new String[5];
//array initialization
names[0]="Chaitanya";
names[1]="Ajeet";
names[2]="Rahul";
names[3]="Shivam";
names[4]="Rohit";
//print array elements
for(int i=0;i<names.length;i++)
System.out.println("names["+i+"]: "+names[i]);
}
}

Program 2
public class JavaExample{
public static void main(String args[]){
//two rows and three columns
int arr[][]={{11,22,33},{44,55,66}};
//outer loop 0 till number of rows
for(int i=0;i<2;i++){
//inner loop from 0 till number of columns
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
//new line after each row
System.out.println();
}
}
}

Program 3
public class JavaExample{
public static void main(String args[]){
//String array
String names[]={"Chaitanya", "Ajeet", "Rahul", "Hari"};
//print array elements using for-each loop
for(String str:names)
System.out.println(str);
//int array
int numbers[]={1, 2, 3, 4, 5};
//print array elements using for-each loop
for(int num:numbers)
System.out.println(num);
}
}

Program 4
public class JavaExample{
public static void main(String args[]){
int number[]={1, 5, 7, 9, 11};
for(int i=0;i<=number.length;i++){
System.out.println(number[i]);
}
}
}

Program 5
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5, 2, 5};
// access each array elements
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}

Program 6
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5};
// loop through the array
// using for loop
System.out.println("Using for Loop:");
for(int i = 0; i < age.length; i++) {
System.out.println(age[i]);
}
}
}

Program 7
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5};
// loop through the array
// using for loop
System.out.println("Using for-each Loop:");
for(int a : age) {
System.out.println(a);
}
}
}

Program 8
class Main {
public static void main(String[] args) {
int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12};
int sum = 0;
Double average;
// access all elements using for each loop
// add each element in sum
for (int number: numbers) {
sum += number;
}
// get the total number of elements
int arrayLength = numbers.length;
// calculate the average
// convert the average from int to double
average = ((double)sum / (double)arrayLength);
System.out.println("Sum = " + sum);
System.out.println("Average = " + average);
}
}

Program 9
Buatlah sebuah array dengan Java yang bisa memasukkan data sebagai berikut:
Bilangan A Bilangan B Bilangan C D = A x B E = B/C F = (A+B)xC

Catatan:
Yang dimasukkan hanya bilangan A,B dan C.
D,E,F diperoleh dari hasil perhitungan A,B, dan C.

You might also like