import java.util.Arrays;
import java.util.List;
import java.util.Random;

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){ 

    }  

    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 -10000; // added so code would compile
    }

     public boolean equals(Object aThat) { // aThat is a DynamicArrayOfInts object

       return false;  // added so code would compile
    } 

    public boolean equals(List<Integer> list)  { // list is a LinkedList, or ArrayList, etc

       return false;  // added so code would compile
    }

    public int get(int position){

       return -10000;  // added so code would compile
    }

    public void set(int index, int value){

    }

    public void insertAt(int index, int value) {

    }

    public void add(int value) { 

    }

    public void removeAt(int index) {

    }

    public void printAll() {

    }

    public static void main(String args[]) {
       DynamicArrayOfInts list1 = new DynamicArrayOfInts();
       list1.insertAt(0,1);
       list1.insertAt(1,2);
       list1.add(3);
       // list1 is 1, 2, 3
       System.out.print("list1: "); list1.printAll();
       list1.set(2,100);
       // list1 1 is 1, 2, 100
       //System.out.print("list1: "); list1.printAll();
       System.out.println("list1[2]=" + list1.get(2));
       list1.removeAt(2);
       // list1 is 1, 2
       System.out.print("list1: "); list1.printAll();    
       DynamicArrayOfInts list2 = new DynamicArrayOfInts();
       list2.insertAt(0,2);
       list2.insertAt(0,3);     
       list2.insertAt(0,1);
       list2.removeAt(1);
       // list2 is 1, 2
       System.out.print("list2: ");list2.printAll();
       System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());
       // list1 and list2 are equal
       System.out.println("list1 equals list2 is " + list1.equals(list2));
       list2.insertAt(2,3);
       // list2 is 1, 2, 3
       System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());       
       // list1 and list2 are not equal
       System.out.println("list1 equals list2 is " + list1.equals(list2));  
         ArrayList list3 = new ArrayList();
         list3.add(1);list3.add(2);list3.add(3);
         System.out.println("list2 equals list3 is " + list2.equals(list3));           
    }    
} 

so this is the code i am working on for a project. i am supposed to do this for the following method:

method "public boolean equals(Object aThat){": returns true if the int elements in list equal the int elements in the dynamic array. Note that if the size of the list and the size of the dynamic array are not the same, this method can return false without checking the equality of the elements.

the question i have is how to use the Object aThat. what exactly is it, and what am i supposed to do to compare it to an array? if it was an array I assume i could just do array.length and compare the two.

Recommended Answers

All 6 Replies

If you cast aThat to a DynamicArrayOfInts object, you could then call the class's methods.

aThat thing = new DynamicArrayOfInts();

like that?

also in the commenting, it says aThat is a DynamicArrayOfInts() object. what does that mean?

a DynamicArrayOfInts() object. what does that mean?

You create an object of a class with the new statement:
AClass aclsRef = new AClasss();

Here the variable aclsRef refers/points to a AClass object.

You cast a variable to a type by putting the type in ()s before the variable:
int x = (int)aVar; // cast aVar to an int

You can read about casting here: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

alright, i think i understand. thank you very much

I am also working on it and I still didn't get it.. can someone clarify bit more please what this sentence is saying

if the size of the list and the size of the dynamic array are not the same, this method can return false without checking the equality of the elements.

thanks

Please start a new thread, don't hijack other's thread.

To answer your question, it means that if 2 lists already have different sizes, it implies that both are not equal (return false). Therefore, you do not need to check each element equality at all. Otherwise, you need to check for equality of each element of both lists.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.