Cheking wheter two arrays are equal

Page 1 of 1

9 Replies - 1008 Views - Last Post: 12 September 2011 - 08:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=246931&amp;s=ac03ce3d660b4eb67b574cd576756a07&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 imu_1

• D.I.C Regular

Reputation: -6
• Posts: 256
• Joined: 03-June 11

Cheking wheter two arrays are equal

Posted 12 September 2011 - 07:06 PM

I am trying to create a generic method which will check to see whether two arrays have equal number of elements and after that it should check whether they have the same elements.

So, I have created a class
``` public class Array
{
T []list;
int numitems;

public Array(int size)
{
list = (T []) new Object[size];
numitems = 0;
}

// return the number of items ( not the length of the array)
public int getSize( )
{
return numitems;

}
public boolean equal(T[] list2)
{
// what I am tryin gto do is first compae whether they have the sam enumber of elements,if so,
// then I should compare the actual elements
// I am not interested in the length of the array since both arrays can have same number of elements
// but of different lengths
// My problem is that how do I compare the number of elements ?

// if(               )
{
for(int i = 0; i < getSize(); i++)
if(list[i] != list2[i])
return false;
// }

return true;
}

public static void main(String args[])
{
Array<Integer> myList = new Array<Integer>(5);
Array<Integer> otherList = new Array<Integer>(5);
for (int i=0; i<5; i++)  // 6th entry causes error message
{
myList.add(i);   // assume we have an add method from the array class
}
}

```

This post has been edited by imu_1: 12 September 2011 - 07:07 PM

Is This A Good Question/Topic? 0

Replies To: Cheking wheter two arrays are equal

#2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11790
• Posts: 44,305
• Joined: 27-December 08

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 07:07 PM

Why not just use the Arrays.equals(Object[] one, Object[] two) method?

#3 imu_1

• D.I.C Regular

Reputation: -6
• Posts: 256
• Joined: 03-June 11

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 07:09 PM

I cant od that based on the requirements: Sorry, I forgot to share that with you.

A method which receives another GenArrList as a parameter, and returns true if the current and passed lists have the same number of elements and all corresponding elements are equal

#4 jon.kiparsky

• Pancakes!

Reputation: 9529
• Posts: 16,478
• Joined: 19-March 11

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 07:10 PM

if you have an array, you can always check its length:
```int[] array = {1,2,3};
System.out.println(array.length);
```

Is that what you're looking for?

#5 <3DIC

• D.I.C Regular

Reputation: 6
• Posts: 327
• Joined: 06-October 10

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 07:14 PM

jon.kiparsky, on 13 September 2011 - 02:10 AM, said:

if you have an array, you can always check its length:
```int[] array = {1,2,3};
System.out.println(array.length);
```

Is that what you're looking for?

As jon says you can get the length of the array that way. Once you have the length of each array, you can simply loop through each array and compare the elements

#6 pbl

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

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 07:16 PM

Won't work
You will have to pass the Array object to your equals method (not just the list) so you will be able to check first if your local numitems == the Array object passed as parameter menuitems

#7 imu_1

• D.I.C Regular

Reputation: -6
• Posts: 256
• Joined: 03-June 11

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 08:17 PM

pbl, you understood my problem.I need to compare the numitems and not the
length of the array. So in my main method, I will write something like this:

System.out.println(myList.equal(otherList));

The in my Array class, it will be like this

public boolean equal(T list2)
{
// then how should I compare the numitems ?
//Is it gonna be like this..

if(this.numitems == list2.numitems)// Here's where I cant figure out...

#8 pbl

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

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 08:20 PM

No will have to be
```public boolean equals(Array otherArray) {
// checking array size
if(numitems != otherArray.numitems) return false;
/// checking list type
if(!list[0] instanceof otherArray.list[0]) return false;

```

then you can compare yout T[] list with otherArray.list

If you do nost pass the whole Array object you wont be able to compare numitems

*Edited: added a check for list[] type

This post has been edited by pbl: 12 September 2011 - 08:28 PM

#9 imu_1

• D.I.C Regular

Reputation: -6
• Posts: 256
• Joined: 03-June 11

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 08:29 PM

ok, so you mean I should write System.out.println(list.equal(list2.list)) instead of just System.out.println(list.equal(list2))??

That would mean passing an array object write?

#10 pbl

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

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 08:36 PM

Based on
```      Array<Integer> myList = new Array<Integer>(5);
Array<Integer> otherList = new Array<Integer>(5);

```

should be

System.out.println(myList.equals(otherList));

```public boolean equals(Array otherArray) {
// checking array size
if(numitems != otherArray.numitems) return false;
/// checking list type
if(!list[0] instanceof otherArray.list[0]) return false;
for(int i = 0; i < numitems; ++i) {
if(!list[i].equals(otherArray.list[i]) return false;
return true;
}

```

or something like that