9 Replies - 776 Views - Last Post: 12 September 2011 - 08:36 PM Rate Topic: -----

#1 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • 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 
          otherList.add(i);
     }   
  } 

 

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  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,153
  • 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?

http://download.orac...til/Arrays.html
Was This Post Helpful? 0
  • +
  • -

#3 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7961
  • View blog
  • Posts: 13,579
  • 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?
Was This Post Helpful? 1
  • +
  • -

#5 <3DIC  Icon User is offline

  • D.I.C Regular


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

Re: Cheking wheter two arrays are equal

Posted 12 September 2011 - 07:14 PM

View Postjon.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
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 imu_1  Icon User is offline

  • D.I.C Regular

Reputation: -6
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • 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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1