-1
import java.util.Scanner;

public class StrictlyIdentical {
 private static Scanner input;

public static void main(String[] args) {
  input = new Scanner(System.in);

  System.out.print("Enter 5 elements for list1:");
  int[] list1 = new int[input.nextInt()];
  for (int i = 0; i < list1.length; i++) {
   list1[i] = input.nextInt();
  }

  System.out.print("Enter 5 elements for list2:");
  int[] list2 = new int[input.nextInt()];
  for (int i = 0; i < list2.length; i++) {
   list2[i] = input.nextInt();
  }

  if (equals(list1, list2)) {
   System.out.println("The two lists are strictly identical");
  } else {
   System.out.println("The two lists are not strictly identical");
  }

 }

 public static boolean equals(int[] list1, int[] list2) {

  if (list1.length != list2.length)
   return false;

  for (int i = 0; i < list2.length; i++) {
   if (list1[i] != list2[i])
    return false;
  }

  return true;

 }
}

This is what I have. When prompted to enter list 1: I enter 1 2 3 4 5. Then when prompted to enter list 2 I enter 1 2 3 4 5. I keep getting the two lists are not strictly identical, however when all the values in the list are equal and are the same values for both lists then I get the two lists are strictly identical.

cguitarw
  • 5
  • 3

4 Answers4

2

The problem is that you are inputing the size of the lists:

int[] list1 = new int[input.nextInt()];

and

int[] list2 = new int[input.nextInt()];

So if you don't enter it before the values, you will have one list of size 1 and the other of size 3.

You can either change the lists to be of fixed size:

int[] list1 = new int[5];

Or change your prompt :

System.out.print("Enter the size of list1 followed by its elements:");

You can also dynamically affect the size of the arrays, but you have to change you code's logic:

System.out.print("Enter elements for list1:");
int[] list1 = Arrays.stream(input.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();

System.out.print("Enter elements for list2:");
int[] list2 = Arrays.stream(input.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
Florent Bayle
  • 9,983
  • 3
  • 31
  • 42
0

In this line:

int[] list1 = new int[input.nextInt()];

You actually create a list of size 1 (At least - that's what you write in the input you give), and here:

list1[i] = input.nextInt();

You enter 2 as it's only value.

Furthermore, your Scanner keeps going in the same line as above - creating a second list of size 3 and entering 4,5 and 1. (Read more here about nextInt() and Scanner)

So, you have two choices:

  • Get user input about size of list before-hand.
  • initialize the array with a value, like: int[] arr = new int[5];
Community
  • 1
  • 1
MordechayS
  • 1,642
  • 2
  • 19
  • 27
0

Set the array 5 and it is solved.

Equals method is working find as well.

      System.out.print("Enter 5 elements for list1:");
      int[] list1 = new int[5];   <<<<
      for (int i = 0; i < list1.length; i++) {
       list1[i] = input.nextInt();
      }

      System.out.print("Enter 5 elements for list2:");
      int[] list2 = new int[5];    <<<<<<
      for (int i = 0; i < list2.length; i++) {
        list2[i] = input.nextInt();
      }
Luminous_Dev
  • 579
  • 5
  • 14
0

I'd suggest that you go for this problem with lists instead of arrays. That opens list1.equals(list2) for comparison instead of writing a no way bullet-proof homebrew equals method ;-)

So try it like this:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StrictlyIdentical {
    private static Scanner input;

public static void main(String[] args) {
    input = new Scanner(System.in);

    System.out.print("Enter 5 elements for list1:");
    List<Integer> list1 = new ArrayList<Integer>();
    for (int i = 0; i < 5; i++) {
        list1.add(input.nextInt());
        }

    System.out.print("Enter 5 elements for list2:");
    List<Integer> list2 = new ArrayList<Integer>();
    for (int i = 0; i < 5; i++) {
        list2.add(input.nextInt());
        }

    if (list1.equals(list2)) {
        System.out.println("The two lists are strictly identical");
        } else {
        System.out.println("The two lists are not strictly identical");
        }
    }
}
Kai Adelmann
  • 199
  • 6
  • 14