• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding to an array- HELP!  RSS feed

 
Naf Rash
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably a really easy question but I have an array and want to add an element to the beginning of it. In other words I have an existing array that contains a number of elemts/values but now I want to add a value to position 0 of the array.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You�re probably better of using a LinkedList rather than an array (if your solution allows such). The LinkedList class has methods for adding elements at the beginning or end. Outside of the LinkedList, you would need to create a new Array as size oldArray.length + 1, insert the item into position 0 of the newArray, then iterate through the old array and add each element to the newArray � depending on the size of your array and the frequency of which you are doing this, it would be rather resource intensive a solution and a LinkListed would be much better.
Based on the last 2 or 3 questions you have posted, might I recommend you take a look at the Java Collections Framework lesson from Sun�s online Java Tutorial. I think you will find it very applicable to the subject you are asking about.
[ March 03, 2004: Message edited by: Mark Vedder ]
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I certainly agree with Mark, but you obviously need to know how to do this.
here is a little sample, also you need to make sure that your array will handle the new element, or else you will get an exception. To ensure this from happening you need to increase your array.lenght to array.length*2.

HTH
Gabe
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabe,
I think you meant for this line:
      myArray[0] = myArray[n];
to be
      myArray[0] =n;
since the first one would store the item at index n in index 0 (and would of course cause an IndexOutOfBOundsException if n is a number greater then myArray.length - 1).
Also, with this method, the element at index 0 is always lost since the method moves the elements at i+1 and (i+1) never equals zero. In a 3 element array, the method moves the items at index 1, 2, & 3 (which is a non-existent element, hence the next issue).
Lastly, in its current state, this method will always throw an ArrayIndexOutOfBoundsException since on the last iteration through the for loop, i = length -1 (i.e. the last index value of the array) and it attempts to add an element to [i+1] which is greater then the last index value of the array. You could resolve the issue by changing the for loop�s test condition to i < myArray.length - 1 but then you will always loose the last element of the array when copying it over.
Therefore a better method would be:

the method would be call with a line similar to:
      myArray = addElement(myArray, 100);
Ultimately though, I think this discussion is all academic since a LinkedList would be a more efficient solution.
Regards,
Mark
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!