adding to a dynamic array size...

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

adding to a dynamic array size...

 
0
  #1
May 6th, 2007
I can't figure how to get this going, for every object I create I want to add that to the existing array. I read about arraycopy but I don't know how to increase the existing size of the array and always end up with array out of index error...

  1. A a1 = new A(1, 2, "test");
  2.  
  3. size = 1;
  4. sizeCounter = 1;
  5. A a[] = new A[size];
  6. a[0] = a1;
  7.  
  8. A a2 = new A(3, 4, "test2");
  9.  
  10. sizeCounter++;
  11. if (sizeCounter>=size){
  12. size++;}
  13.  
  14. A[size-1] = a2;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: adding to a dynamic array size...

 
0
  #2
May 6th, 2007
Why not encompass your existing class around the ArrayList. Then just access the .add() method?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,402
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 255
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: adding to a dynamic array size...

 
0
  #3
May 7th, 2007
iamthwee is right, you should be using ArrayList. You cannot increase the size of an existing array. You have to create a new array that is one larger than the current array, then you can use System.arraycopy to copy the existing elements from the old to the new array, than add the new element.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: adding to a dynamic array size...

 
0
  #4
May 7th, 2007
Doh! Ok, then, I'll just increase the size of the array. I was doing somethig similar to below, everything is done but I thought it would be neat idea to increase the array dynamically.

prompt user for input
checkinput()
if no dupe add to array
else error msg
Last edited by rugae; May 7th, 2007 at 7:44 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: adding to a dynamic array size...

 
0
  #5
May 7th, 2007
Originally Posted by rugae View Post
Doh! Ok, then, I'll just increase the size of the array. I was doing somethig similar to below, everything is done but I thought it would be neat idea to increase the array dynamically.

prompt user for input
checkinput()
if no dupe add to array
else error msg
You obviously missed my post then...
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: pasanindeewara is an unknown quantity at this point 
Solved Threads: 0
pasanindeewara pasanindeewara is offline Offline
Newbie Poster

Re: adding to a dynamic array size...

 
0
  #6
Aug 14th, 2009
Guys you can use

import java.util.List;

List <String> wordset = new ArrayList <String>();

usimg this you can get dynamic arrays working well...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 40
Reputation: harsh2327 is an unknown quantity at this point 
Solved Threads: 7
harsh2327 harsh2327 is offline Offline
Light Poster

Re: adding to a dynamic array size...

 
0
  #7
Aug 14th, 2009
You could even use Vector class.
Its implementation is much simpler.
Its in java.util package
<(^.^)>.....HM.....<(^.^)>
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: eng.M4AH is an unknown quantity at this point 
Solved Threads: 1
eng.M4AH eng.M4AH is offline Offline
Newbie Poster

Re: adding to a dynamic array size...

 
0
  #8
Aug 15th, 2009
Originally Posted by rugae View Post
Doh! Ok, then, I'll just increase the size of the array. I was doing somethig similar to below, everything is done but I thought it would be neat idea to increase the array dynamically.

prompt user for input
checkinput()
if no dupe add to array
else error msg
you need to know that the array that you use in java is not flexible to increase the size of the array by itself, the array is a STATIC data structure.

if you make
  1. int [] arr = new int[10];
this array will remain in your scope of length 10, unless you make a new array that is nor related to that one and of bigger size and then put the elements of the first array in the second array and then make the first array point to the second array.
  1. int [] arr = new int[10];
  2. /* put data in the 10 elements of arr
  3. -----
  4. ----
  5. */
  6. int []arr2 = new int[arr.length * 2];
  7. //copy the elements of arr to the first 10 elements of arr2
  8. arr = arr2;

or you can easily use arrayList as they told you, which already does that.
Last edited by eng.M4AH; Aug 15th, 2009 at 12:14 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC