9 Replies - 482 Views - Last Post: 24 April 2011 - 05:39 AM Rate Topic: -----

#1 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Nested loop check.

Posted 23 April 2011 - 10:33 AM

Hi! i am to write a program to check whether or not two arrays of integers are identical, it runs correctly when the two sizes of the arrays are not equal, but when the two sizes are the same, it doesn't run as suppose, so I guess the problem is my nested loop. Here's my code:

import java.util.Scanner;
public class identicalArrays {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter list1: ");
        int firstList1 = input.nextInt();
        int[] list1 = new int[firstList1];
        for (int i = 0; i < list1.length; i++) {
            firstList1 = input.nextInt();
        }
        System.out.print("Enter list2: ");
        int firstList2 = input.nextInt();
        int[] list2 = new int[firstList2];
        for (int j = 0; j < list2.length; j++) {
            firstList2 = input.nextInt();
        }

        if (equal(list1, list2)) {
            System.out.println("Two lists are identical");
        } else {
            System.out.println("Two lists are not identical");
        }
    }

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

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


This post has been edited by illuss: 23 April 2011 - 10:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Nested loop check.

#2 cyan1de  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 45
  • Joined: 12-February 09

Re: Nested loop check.

Posted 23 April 2011 - 10:47 AM

If the list are equal in size and you are checking to see if each element at the same index is the same than you would not use nested loops. Just use one loop than

if (list1[x] != list2[x])


Was This Post Helpful? 0
  • +
  • -

#3 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Nested loop check.

Posted 23 April 2011 - 11:01 AM

because the elements in the two arrays are not in order, so what i'm trying to do is use linear searh, i use the outer loop to get the key element, then use the nested loop to find if the key element is in the other array.
Was This Post Helpful? 0
  • +
  • -

#4 cyan1de  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 45
  • Joined: 12-February 09

Re: Nested loop check.

Posted 23 April 2011 - 11:09 AM

View Postilluss, on 23 April 2011 - 11:01 AM, said:

because the elements in the two arrays are not in order, so what i'm trying to do is use linear searh, i use the outer loop to get the key element, then use the nested loop to find if the key element is in the other array.


That is not what you are doing. Right now you are comparing if every element of k is equal is equal to each index of x. The only way this will return true is if every element in both arrays are equal. Just sort the list and compare them.

This post has been edited by cyan1de: 23 April 2011 - 11:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Nested loop check.

Posted 23 April 2011 - 11:11 AM

oh i see. So what changes would you suggest for me to make this work?
Was This Post Helpful? 0
  • +
  • -

#6 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Nested loop check.

Posted 23 April 2011 - 11:32 AM

oh sorry i didn't see your last sentence there. unfortunately
We're not yet allowed to use sort :(
Was This Post Helpful? 0
  • +
  • -

#7 cyan1de  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 45
  • Joined: 12-February 09

Re: Nested loop check.

Posted 23 April 2011 - 11:35 AM

This

	public static void main(String [] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter list1: ");
		int firstList1 = input.nextInt();
		int[] list1 = new int[firstList1];
		for (int i = 0; i < list1.length; i++)
		{
			firstList1 = input.nextInt();
		}
		System.out.print("Enter list2: ");
		int firstList2 = input.nextInt();
		int[] list2 = new int[firstList2];
		
		for (int j = 0; j < list2.length; j++)
		{
			firstList2 = input.nextInt();
		}

		Arrays.sort(list1);
		Arrays.sort(list2);

		if (equal(list1, list2))
		{
			System.out.println("Two lists are identical");
		}
		else
		{
			System.out.println("Two lists are not identical");
		}

	}

	public static boolean equal(int[] list1, int[] list2)
	{
		if (list1.length == list2.length)
		{
			for (int x = 0; x < list1.length; x++)
			{
				if (list1[x] != list2[x])
				{
					return false;
				}
			}
		}
		return true;
	}


This post has been edited by cyan1de: 23 April 2011 - 11:36 AM

Was This Post Helpful? 1
  • +
  • -

#8 cyan1de  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 45
  • Joined: 12-February 09

Re: Nested loop check.

Posted 23 April 2011 - 11:54 AM

I guess something like this than.
	public static boolean equal(int[] list1, int[] list2)
	{
		boolean present = false;

		if (list1.length == list2.length)
		{
			for (int x = 0; x < list1.length; x++)
			{
				for (int z = 0; z < list1.length; z++)
				{
					if (list1[x] == list2[z])
					{
						present = true;
					}
				}
				if (present == false)
					return false;
				else 
					present = false;
			}
		}
		return true;
	}


Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Nested loop check.

Posted 23 April 2011 - 02:43 PM

You will need more than that
right now if list1 is 1,1,1,1,1
and list2 is 1,2,3,4,5

your method will return true

and you will need two loops to compare first list1 with list2 and list2 with list1 or your algorithm will fail if list1 is 1,1,1,2,2 and list2 is 2,2,2,1,1

so you have to count how many times each element of list1 is present 1) in list1 2) in list2
if no match return false

something like

static boolean equals(int[]list1, int[] list2) {
    if(list1.length != list2.length)
       return false;

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


Happy coding
Was This Post Helpful? 1
  • +
  • -

#10 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Nested loop check.

Posted 24 April 2011 - 05:39 AM

oh that is really helpful!! and in case you guys have not notice, one of my mistake was in the main method i did not save any input into the two arrays, so it didn't run correctly.
Thanks a lot you guys! Cheers!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1