Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why getting a strange outcome while trying to print Array method? outcome: [I@6f539caf

I’m trying to print the first index of the first Array and the last index of the second in the third Array. And also tried different methods but still failed.

i also tried this code: System.out.println("Third Array will be : " +Arrays.toString( combinationOFTwoArrays(firstArray, secondArray)));
and out come is:Third Array will be: [0,0,0,0,0]

Any solution for this? I really appreciate any help you can provide.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private static int[] combinationOFTwoArrays(int[] firstArray, int[] secondArray) {

    int firstElementOfFirstArray = firstArray[0];
    int lastElementOfSecondArray = secondArray[secondArray.length -1];

    int[] thirdArray = new int[firstElementOfFirstArray + lastElementOfSecondArray];

    return thirdArray;
}

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter length of Array: ");
    int  firstArrayLenght = sc.nextInt();
    int[] firstArray = new int[firstArrayLenght];

    System.out.println("Enter length of Array: ");
    int  secondArrayLenght = sc.nextInt();
    int[] secondArray = new int[secondArrayLenght];

    System.out.println("Enter first Array: ");
    for (int i = 0; i<firstArrayLenght; i++) {
        firstArray[i] = sc.nextInt();
    }

    System.out.println("Enter Second Array: ");
    for (int k = 0; k<secondArrayLenght; k++) {
        secondArray[k] = sc.nextInt();
    }

    System.out.println("Third Array will be : " + combinationOFTwoArrays(firstArray, secondArray));
}

outcome:

Enter length of Array: 
4
Enter length of Array: 
4
Enter first Array: 
 1 2 3 4 
Enter Second Array: 
5 6 7 8
Third Array will be : [I@6f539caf

Process finished with exit code 0

>Solution :

I guess you mean something like this:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter length of Array: ");
        int firstArrayLenght = sc.nextInt();
        int[] firstArray = new int[firstArrayLenght];

        System.out.println("Enter length of Array: ");
        int secondArrayLenght = sc.nextInt();
        int[] secondArray = new int[secondArrayLenght];

        System.out.println("Enter first Array: ");
        for (int i = 0; i < firstArrayLenght; i++)
            firstArray[i] = sc.nextInt();

        System.out.println("Enter Second Array: ");
        for (int k = 0; k < secondArrayLenght; k++)
            secondArray[k] = sc.nextInt();

        System.out.println("Third Array will be : " + Arrays.toString(combinationOFTwoArrays(firstArray, secondArray)));
    }

    private static int[] combinationOFTwoArrays(int[] firstArray, int[] secondArray) {

        int firstElementOfFirstArray = firstArray[0];
        int lastElementOfSecondArray = secondArray[secondArray.length -1];

        int[] thirdArray = new int[2];
        thirdArray[0] = firstElementOfFirstArray;
        thirdArray[1] = lastElementOfSecondArray;
        return thirdArray;
    }
}

You have to add

Arrays.toString

in your output line, because the function returns int[ ]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading