Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with dynamic array method

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Help with dynamic array method

    I'm trying to write a method for a dynamic array class that compares the size of the current dynamic array to the one passed in from the method. The issue i'm having is that the type in the method argument is of type Object, and while it is assumed to be of the same type as the array i'm comparing it to, Java doesn't know that. I've tried casting it, but it doesn't seem to be working. Any help would be greatly appreciated. Here's the basic method header also it should be noted that the object using this method is a DynamicArrayOfInts object and I have to use Object aThat in the argument for the method:

     public boolean equals(Object aThat) { // aThat is a DynamicArrayOfInts object
     
    		 if(this.size!=aThat.size)  //i know this is wrong
                     return false;  // added so code would compile
        }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with dynamic array method

    What did you try casting to what and where in the code did that take place?
    public boolean equals(Object aThat) { // aThat is a DynamicArrayOfInts object
          if(this.size!=aThat.size) {  //i know this is wrong
               return false;
          }
          return true; //Return added for the code path you did not return on.
        }

    With a minor fix, and the fact the method is only a comparison of one field, I see nothing wrong with it.
    Post the code and add comments where you are making changes or have questions or errors.
    "It doesn't seem to be working" does not tell much about what you are getting or what you expected, try to be more descriptive in saying what is not working the way you wanted.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Help with dynamic array method

    I'm getting an error that says aThat.size cannot be resolved to a field. I assumed it was because the Object class doesn't have a distinct size field where as my DynamicArrayOfInts class does have that field. I was trying to cast as either (DynamicArrayOfInts)aThat.size or (Integer)aThat.size because it's an Integer array. I received the same not a valid field error. I should add that after determining if their sizes are equal I have to compare each value in both to make sure they are the same arrays.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with dynamic array method

    You get an error that says .size cannot be resolved because the Object class does not have a size variable visible to your class.

    So long as aThat is of type object, aThat.size will not work. If you cast aThat to a type that has .size, and is visible, you could use it that way, or using a method call like sizeOf() or length() rather than .size

    It is hard to say without seeing the code.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Help with dynamic array method

    I did try type casting to the DynamicArrayOfInts class buts it says it cannot compare types int and DynamicArrayOfInts. The class up to this point looks like this:


    class DynamicArrayOfInts {
     
        private int[] storage;
        private int size;
        private final  int INITIAL_CAPACITY = 8;
        private final int GROW_FACTOR = 2;
     
        public DynamicArrayOfInts() {
          storage = new int[INITIAL_CAPACITY]; 
          size = 0;
        }
     
        private void rangeCheck(int index){ 
        	if(index<0 || index>=this.size)
        		throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
     
        }  
     
        private void ensureCapacity(int size_wanted) {
          int max_capacity = storage.length;
          if (size_wanted > max_capacity) {
            max_capacity = max_capacity * GROW_FACTOR +1; 
            storage = Arrays.copyOf(storage, max_capacity); // increases array size + copy contents
          } 
        }
     
        public int size() {
     
           return size; // added so code would compile
        }
     
    	 public boolean equals(Object aThat) { // aThat is a DynamicArrayOfInts object
     
    		 if(this.size!=((DynamicArrayOfInts)aThat.size))
              return false;  // added so code would compile
     
        }

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Help with dynamic array method

    In Java the '(typecast)' operator has lower precedence than the '.' member operator.

    Try comparing the following int:
    this.size // Or simply size
    with the following int
    ((DynamicArrayOfInts)aThat).size()

    Because of the presence of the outer parentheses, this first casts aThat to a DynamicArrayOfInts object and then calls its size() method.

    You will have to change the equals() method so that it always returns something.




    Cheeers!

    Z
    Last edited by Zaphod_b; September 27th, 2012 at 01:33 PM.

Similar Threads

  1. Contain method to read array
    By theaveguy in forum Loops & Control Statements
    Replies: 3
    Last Post: February 27th, 2012, 12:32 AM
  2. Contain method to read array
    By theaveguy in forum Java Theory & Questions
    Replies: 8
    Last Post: February 25th, 2012, 02:53 PM
  3. please need help ... for the delete method in array
    By yanikapausini:) in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2012, 03:22 PM
  4. Array Search Method
    By Kyuubi426 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 08:31 PM
  5. dynamic method problem...
    By Ranger-Man in forum Object Oriented Programming
    Replies: 8
    Last Post: September 7th, 2009, 04:22 PM