Java Program to swap two arrays Example - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this Java programming tutorials, I am going to show you how to swap two arrays in Java.
Input 1 : Give an integer array from command line. array1 {1,,2,4,5,3,7}
Input 2 : Give an another integer array from command line. array2 {5,6,0,8,4,3}
Output : array1 : {5,6,0,8,4,3} and array2 : {1,,2,4,5,3,7}
package com.onlinetutorialspoint.javaprograms;

import java.util.Scanner;

public class SwappingTwoArrays {
    public static void main(String[] args) {
        Scanner input_size = new Scanner(System.in);
        System.out.println("Enter the Size of Arrays : ");
        int size = input_size.nextInt();
        int[] array1 = new int[size], array2 = new int[size], buffer = new int[size];
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the First Array of Elements: ");
        for (int i = 0; i < size; i++) {
            array1[i] = sc.nextInt();
        }
        System.out.println("Enter the Second Array of Elements: ");
        for (int i = 0; i < size; i++) {
            array2[i] = sc.nextInt();
        }
        System.out.println("Before Swapping");
        System.out.println("First Array: ");
        for (int i = 0; i < size; i++) {
            System.out.print(array1[i]);
        }
        System.out.println("\nSecond Array: ");
        for (int i = 0; i < size; i++) {
            System.out.print(array2[i]);
        }
        for (int i = 0; i < size; i++) {
            buffer[i] = array1[i];
            array1[i] = array2[i];
            array2[i] = buffer[i];
        }
        System.out.println("\nArrays after Swapping");
        System.out.println("First Array: ");
        for (int i = 0; i < size; i++) {
            System.out.print(array1[i]);
        }
        System.out.println("\nSecond Array: ");
        for (int i = 0; i < size; i++) {
            System.out.print(array2[i]);
        }
    }
}

Output
Enter the Size of Arrays :
5
Enter the First Array of Elements:
1
5
6
9
8
Enter the Second Array of Elements:
5
6
4
2
8
Before Swapping
First Array:
15698
Second Array:
56428
Arrays after Swapping
First Array:
56428
Second Array:
15698
Happy Learning 🙂

Binary To Decimal Conversion Java Program
Binary To Hexadecimal Conversion Java Program
Binary Search using Java
Java Program to Find the GCD of Two Numbers
Java Program to Check a Number is Palindrome or not ?
How to Rotate an Array to Left direction based on user input ?
Java Program for Reverse Of Number
What is Java Arrays and how it works ?
Java Program for String Sorting Example
Java Program to Print Pattern Triangle
Java Program to Print Diamond Pattern
Java Program For Binary Addition
Java program to find sum of digits
Program to display Java Multiplication Table
Java program for Triangle Part – 2

Binary To Decimal Conversion Java Program
Binary To Hexadecimal Conversion Java Program
Binary Search using Java
Java Program to Find the GCD of Two Numbers
Java Program to Check a Number is Palindrome or not ?
How to Rotate an Array to Left direction based on user input ?
Java Program for Reverse Of Number
What is Java Arrays and how it works ?
Java Program for String Sorting Example
Java Program to Print Pattern Triangle
Java Program to Print Diamond Pattern
Java Program For Binary Addition
Java program to find sum of digits
Program to display Java Multiplication Table
Java program for Triangle Part – 2






Naina
July 14, 2019 at 8:06 am - Reply 

How can we swap 133967 in java










Naina
July 14, 2019 at 8:06 am - Reply 

How can we swap 133967 in java




How can we swap 133967 in java