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

Array Manipulation Examples

This document contains examples of array manipulation in Java including bubble sort, matrix display, array sum, finding the maximum element, swapping arrays, sorting arrays, binary search, filling arrays, copying arrays, and comparing arrays for equality. The examples demonstrate basic operations on arrays such as sorting, searching, summing elements, and copying/comparing arrays.

Uploaded by

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

Array Manipulation Examples

This document contains examples of array manipulation in Java including bubble sort, matrix display, array sum, finding the maximum element, swapping arrays, sorting arrays, binary search, filling arrays, copying arrays, and comparing arrays for equality. The examples demonstrate basic operations on arrays such as sorting, searching, summing elements, and copying/comparing arrays.

Uploaded by

rabexa4689
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

//Array Manipulation Examples

public class BubbleSort {


public static void main(String[] args) {
int[] arr = {5, 2, 8, 3, 1, 6};
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}

public static void bubbleSort(int[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
//Matrix display
public class MatrixDisplay {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}
//Array Sum

public class ArraySum {


public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
for (int[] row : matrix) {
for (int element : row) {
sum += element;
}
}
System.out.println("Sum of all elements: " + sum);
}
}
//Max element
public class MaxElement {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int max = matrix[0][0];
for (int[] row : matrix) {
for (int element : row) {
if (element > max) {
max = element;
}
}
}
System.out.println("Maximum element: " + max);
}
}
//Swap Arrays
public class SwapArrays {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {6, 7, 8, 9, 10};

// Print the arrays before swapping


System.out.println("Array 1 before swapping: " +
Arrays.toString(array1));
System.out.println("Array 2 before swapping: " +
Arrays.toString(array2));

// Swap the arrays


int[] temp = array1;
array1 = array2;
array2 = temp;
// Print the arrays after swapping
System.out.println("Array 1 after swapping: " +
Arrays.toString(array1));
System.out.println("Array 2 after swapping: " +
Arrays.toString(array2));
}
}
//Sort array
import java.util.Arrays;

public class SortArray {


public static void main(String[] args) {
int[] array = {5, 3, 1, 4, 2};
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}
}
import java.util.Arrays;

public class SortExample {


public static void main(String[] args) {
int[] numbers = { 5, 2, 9, 1, 5, 6 };
Arrays.sort(numbers);
System.out.println("Sorted array in ascending order: " +
Arrays.toString(numbers));
}
}
//Arrays Binary search
import java.util.Arrays;

public class BinarySearchExample {


public static void main(String[] args) {
int[] numbers = { 1, 2, 3, 4, 5, 6 };
int key = 4;
int index = Arrays.binarySearch(numbers, key);
System.out.println("Index of " + key + " in the array: " + index);
}
}
//Fill
import java.util.Arrays;

public class FillExample {


public static void main(String[] args) {
int[] numbers = new int[5];
Arrays.fill(numbers, 10);
System.out.println("Array filled with 10s: " +
Arrays.toString(numbers));
}
}
//Copyof
import java.util.Arrays;

public class CopyOfExample {


public static void main(String[] args) {
int[] numbers = { 1, 2, 3, 4, 5 };
int[] copy = Arrays.copyOf(numbers, 3);
System.out.println("Original array: " + Arrays.toString(numbers));
System.out.println("Copied array: " + Arrays.toString(copy));
}
}
//Equals
import java.util.Arrays;

public class EqualsExample {


public static void main(String[] args) {
int[] array1 = { 1, 2, 3 };
int[] array2 = { 1, 2, 3 };
int[] array3 = { 1, 2, 4 };
boolean equal1 = Arrays.equals(array1, array2);
boolean equal2 = Arrays.equals(array1, array3);
System.out.println("Are array1 and array2 equal? " + equal1);
System.out.println("Are array1 and array3 equal? " + equal2);
}
}

You might also like