Results 1 to 14 of 14
- 09-29-2012, 10:15 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 10
- Rep Power
- 0
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:
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.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 }
-
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.
- 09-30-2012, 03:04 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 10
- Rep Power
- 0
Re: Return the current size of dynamic array
I thinking using size = storage.length; then return size. I think it should do the work
-
Re: Return the current size of dynamic array
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.
- 09-30-2012, 03:49 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 10
- Rep Power
- 0
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) { }
-
Re: Return the current size of dynamic array
If this were my code, I'd simply do:
Nothing more and nothing less. That's why I was afraid that you were over-thinking this.Java Code:public int size() { return size; }
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.
- 09-30-2012, 04:58 AM #7
Member
- Join Date
- Sep 2012
- Posts
- 10
- Rep Power
- 0
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:
I'm glad I learning!!!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)); } }Last edited by RozenKristal; 09-30-2012 at 05:03 AM.
-
Re: Return the current size of dynamic array
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 you are too!I'm glad I learning!!!
- 09-30-2012, 07:04 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 10
- Rep Power
- 0
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.
- 09-30-2012, 08:19 AM #10
Member
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
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
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 >_>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; }
- 09-30-2012, 01:14 PM #11
Re: Return the current size of dynamic array
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-30-2012, 05:14 PM #12
Member
- Join Date
- Sep 2012
- Posts
- 10
- Rep Power
- 0
Re: Return the current size of dynamic array
- 09-30-2012, 07:02 PM #13
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-01-2012, 08:04 PM #14
Member
- Join Date
- Sep 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
dynamic size of applet
By Mike Petrovich in forum Java AppletsReplies: 9Last Post: 07-11-2012, 03:12 AM -
Get current Frame Size? Autoadjusting GUI to frame size?
By Zigster in forum AWT / SwingReplies: 7Last Post: 05-26-2012, 07:07 PM -
Dynamic array.
By kapil1089theking in forum New To JavaReplies: 1Last Post: 11-04-2011, 05:51 PM -
How can i get a string value in return of a thread class to use to show current time?
By asifzbaig in forum AWT / SwingReplies: 4Last Post: 05-08-2011, 09:00 PM -
How increase dynamic array?
By artemff in forum New To JavaReplies: 5Last Post: 01-01-2010, 12:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks