Results 1 to 14 of 14
  1. #1
    RozenKristal is offline Member
    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Default Return the current size of dynamic array

    My assignment require me to fill in empty methods that the professor gave. First I want to ask you guys to help me explain what I need to do, not giving me the code :( Here what I have so far:
    Java Code:
    import java.util.ArrayList;
    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){
          if((index < 0)||(index == size)||(index > 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 ; // added so code would compile
        }
    at public int size, I suppose to return the current size of the dynamic array. I am a little lost right now as to determine how I count the index as this array is dynamic. Please give me some hints.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Return the current size of dynamic array

    I think that he wants you to return the value held by one of the class's fields. Don't try to over-think this very simple method. As for determining the size, that will likely be done by changing the value held by size when items are added to or removed from this object.

  3. #3
    RozenKristal is offline Member
    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Default Re: Return the current size of dynamic array

    I thinking using size = storage.length; then return size. I think it should do the work

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Return the current size of dynamic array

    Quote Originally Posted by RozenKristal View Post
    I thinking using size = storage.length; then return size. I think it should do the work
    No. That's too complicated. Seriously, think even simpler than that. Besides the size shouldn't be the storage length which is better described as the capacity. The size will be not how big the tank is, but rather how much gas is in the tank. Again this is something that other methods that you haven't shown us will likely manipulate -- the add and remove methods.

  5. #5
    RozenKristal is offline Member
    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Default Re: Return the current size of dynamic array

    Because for that particular method, the direction of the assignment is:
    2. method "public int size()": returns the current size of the dynamic array.

    This is why I just try to use the array.length to return the size. I mean, what else is there besides of this?

    At this moment, add and remove are blank too.
    Java Code:
    public void add(int value) { 
    
        }
    
        public void removeAt(int index) {
    
        }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Return the current size of dynamic array

    Quote Originally Posted by RozenKristal View Post
    Because for that particular method, the direction of the assignment is:
    2. method "public int size()": returns the current size of the dynamic array.

    This is why I just try to use the array.length to return the size. I mean, what else is there besides of this?
    If this were my code, I'd simply do:

    Java Code:
    public int size() {
       return size;
    }
    Nothing more and nothing less. That's why I was afraid that you were over-thinking this.

    Then when you add an item to the array, you increment size, and when you remove an item you decrement size, and voilą, size is automatically correct.

  7. #7
    RozenKristal is offline Member
    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Default Re: Return the current size of dynamic array

    I see where you going with this... since size is a global variable. Increment and decrement size will change the variable around constantly, that turn out I dont need to do the extra work of getting the length of the array, correct?

    Here the skeleton code:
    Java Code:
    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));	       
        }    
    }
    I'm glad I learning!!!
    Last edited by RozenKristal; 09-30-2012 at 05:03 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Return the current size of dynamic array

    Quote Originally Posted by RozenKristal View Post
    I see where you going with this... since size is a global variable. Increment and decrement size will change the variable around constantly, that turn out I dont need to do the extra work of getting the length of the array, correct?
    Something like that. Actually size is a class field not a "global variable" since Java doesn't have the latter.

    And it's not even a matter not doing the extra work of getting the length of the array, returning the length of the array would be inappropriate here, because again you'd be returning the capacity of the gas tank not what you really want to return -- how much gas is in the tank.

    I'm glad I learning!!!
    I'm glad you are too!

  9. #9
    RozenKristal is offline Member
    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Default Re: Return the current size of dynamic array

    But here is what I worry:

    Those two are the description of 2 methods:
    1. method "private void rangeCheck(int index)": if the value of index is less than 0 or greater than or equal to the size of the dynamic array, then throw new exception IndexOutOfBoundsException("Index: " + index + ", Size: " + size);

    2. method "public int size()": returns the current size of the dynamic array.

    I understand what you say for size, but somehow I cant still make a connection of how to use "size" in this assignment.

  10. #10
    huupderitis is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Return the current size of dynamic array

    I'm not sure if it is possible because I am somewhat of a noobie..... but if you REALLY wanted to......

    you could create a rather massive array like say 100 or so slots

    int[100]; naw mean?

    Then you could fill it in.... and then for every slot that has a null... thats how many you dont have

    So in other words for filling it you'd be like arrayName[0], arrayName[1] .... and so on... using a for statement

    Then if you want to see how large it is

    Java Code:
    public int size(int[] yourArrray)
    {
        int counter=0;
       for(int i = 0; i < yourArray.length; i++)
        {
         if(yourArray[i] !=null)
        {
        counter++;
        }
         }
      return counter;
    }
    LOL i don't know if this would work or not? some "experts" would have to check me on this.... but i believe it will just give u a counter on how many non-null slots there are? If that is what you were going for.... and of course you know if there isn't enough room then u can always increase it copying your array and what not.... but to copy it every two times seems like a lot of unneccessary funny business to me >_>

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Return the current size of dynamic array

    Quote Originally Posted by huupderitis View Post
    you could create a rather massive array like say 100 or so slots

    int[100]; naw mean?

    Then you could fill it in.... and then for every slot that has a null... thats how many you dont have
    First, an array with 100 elements isn't in any way massive. Secondly, and more important, an array of a primitive type can never hold nulls. A newly created array contains elements that have the default value for that type: null for Object types, 0 for numeric types and false for booleans.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    RozenKristal is offline Member
    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Default Re: Return the current size of dynamic array

    Quote Originally Posted by huupderitis View Post
    I'm not sure if it is possible because I am somewhat of a noobie..... but if you REALLY wanted to......

    you could create a rather massive array like say 100 or so slots

    int[100]; naw mean?

    Then you could fill it in.... and then for every slot that has a null... thats how many you dont have

    So in other words for filling it you'd be like arrayName[0], arrayName[1] .... and so on... using a for statement

    Then if you want to see how large it is

    Java Code:
    public int size(int[] yourArrray)
    {
        int counter=0;
       for(int i = 0; i < yourArray.length; i++)
        {
         if(yourArray[i] !=null)
        {
        counter++;
        }
         }
      return counter;
    }
    LOL i don't know if this would work or not? some "experts" would have to check me on this.... but i believe it will just give u a counter on how many non-null slots there are? If that is what you were going for.... and of course you know if there isn't enough room then u can always increase it copying your array and what not.... but to copy it every two times seems like a lot of unneccessary funny business to me >_>
    ^^ cuz what my assignment is making a dynamic array that expand its size when the capacity is full, by making a new array bigger with a definite growth factor, then copy content from old to new. Plus the size has to goes up and down when the values more or less.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Return the current size of dynamic array

    You could see how the professionals did it, in the source of java.util.ArrayList, which you can find in the src.zip file in your JDK installation folder.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    RozenKristal is offline Member
    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Default Re: Return the current size of dynamic array

    turn out the size is the max capacity of the array.

Similar Threads

  1. dynamic size of applet
    By Mike Petrovich in forum Java Applets
    Replies: 9
    Last Post: 07-11-2012, 03:12 AM
  2. Replies: 7
    Last Post: 05-26-2012, 07:07 PM
  3. Dynamic array.
    By kapil1089theking in forum New To Java
    Replies: 1
    Last Post: 11-04-2011, 05:51 PM
  4. Replies: 4
    Last Post: 05-08-2011, 09:00 PM
  5. How increase dynamic array?
    By artemff in forum New To Java
    Replies: 5
    Last Post: 01-01-2010, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •