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

Array 2D

The document contains 4 code examples demonstrating operations on arrays in Java: 1. Sorting elements of a 2-dimensional array by reading input from the user and comparing elements to swap them. 2. Sorting elements of a string array using the built-in Arrays.sort method and printing the sorted array. 3. Calculating the sum and average of elements in an integer array by iterating through it and adding elements to a running sum. 4. Performing operations like printing, summing, and finding the maximum value on a double array by iterating through it and applying the respective operations.

Uploaded by

andijunita
Copyright
© © All Rights Reserved
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)
49 views

Array 2D

The document contains 4 code examples demonstrating operations on arrays in Java: 1. Sorting elements of a 2-dimensional array by reading input from the user and comparing elements to swap them. 2. Sorting elements of a string array using the built-in Arrays.sort method and printing the sorted array. 3. Calculating the sum and average of elements in an integer array by iterating through it and adding elements to a running sum. 4. Performing operations like printing, summing, and finding the maximum value on a double array by iterating through it and applying the respective operations.

Uploaded by

andijunita
Copyright
© © All Rights Reserved
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/ 4

1.

Array 2D
Source :
import java.io.*;

class SortArray
{
public static void main(String args[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("\nEnter the no. of rows: "); //enter number of rows


int m = Integer.parseInt(br.readLine());
System.out.print("\nEnter the no. of columns: "); //enter number of columns
int n = Integer.parseInt(br.readLine());

int A[][] = new int[m][n]; //creating a 2D array

//enter elements in 2D Array


System.out.println();
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print("Enter the elements: ");
A[i][j] = Integer.parseInt(br.readLine());
}
}

//Printing the original 2D Array


System.out.println("\nThe original array: ");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(A[i][j] + "\t");
}
System.out.println();
}

//Sorting the 2D Array


int t = 0;
for (int x = 0; x < m; x++)
{
for (int y = 0; y < n; y++)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (A[i][j] > A[x][y])
{
t = A[x][y];
A[x][y] = A[i][j];
A[i][j] = t;
}
}
}
}
}

//Printing the sorted 2D Array


System.out.println("\nThe Sorted Array:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(A[i][j] + "\t");
}
System.out.println();
}
}
}

2. Array short
Source :
import java.util.Arrays;

class ArraySort
{
// This program is the example of array sorting
public static void main(String[] args)
{
// TODO Auto-generated method stub
// initializing unsorted array
String iArr[] = {"Programming", "Hub", "Alice", "Wonder", "Land"};

// sorting array
Arrays.sort(iArr);

// let us print all the elements available in list


System.out.println("The sorted array is:");
for (int i = 0; i < iArr.length; i++)
{
System.out.println(iArr[i]);
}
}
}
3. Array Sum dan Average
Source :
import java.io.*;

class ArrayAverage
{
public static void main(String[] args)
{
//define an array
int[] numbers = new int[]{10, 20, 15, 25, 16, 60, 100};

int sum = 0;

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


{
sum = sum + numbers[i];
}

double average = sum / numbers.length;


System.out.println("Sum of array elements is : " + sum);
System.out.println("Average value of array elements is : " + average);
}
}

4. Array Operations
Source :
class ArrayOperations
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};

// Print all the array elements


for (int i = 0; i < myList.length; i++)
{
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++)
{
total += myList[i];
}

System.out.println("Total is " + total);

// Finding the largest element


double max = myList[0];
for (int i = 1; i < myList.length; i++)
{
if (myList[i] > max)
max = myList[i];
}

System.out.println("Max is " + max);


}
}

You might also like