14 Replies - 446 Views - Last Post: 02 October 2012 - 08:04 PM Rate Topic: -----

#1 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 346
  • Joined: 29-September 12

Add value to an array element

Posted 02 October 2012 - 06:21 PM

So if I want to give a value to one element at some specific index, all I need to do is:
array[index]= value;
?? I dont need to loop right? What kind of data structure need a loop to operate?
Is This A Good Question/Topic? 0
  • +

Replies To: Add value to an array element

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Add value to an array element

Posted 02 October 2012 - 06:37 PM

I always prefered ArrayLists. I would look up a tutorial on them, they are much simpler then arrays. Also, you question is very unclear.
Was This Post Helpful? 1
  • +
  • -

#3 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Add value to an array element

Posted 02 October 2012 - 06:37 PM

Yes, you are correct. Bear in mind though that arrays have a definite length so adding a value on an index greater than the length of an array will throw an exception. If you to add lots of stuff and don't mind adding within the current bounds of the collection, use an ArrayList. It will add an element provided you put something within the ArrayList. On another hand, if you really want to add anywhere, I think you need to use a Map.
Was This Post Helpful? 1
  • +
  • -

#4 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 346
  • Joined: 29-September 12

Re: Add value to an array element

Posted 02 October 2012 - 06:40 PM

I forgot to add that I making a dynamic array using normal array and mimic arraylist, by making bigger array and copy the values over when the capacity is full. hence the reason why I am doing this.
Was This Post Helpful? 0
  • +
  • -

#5 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Add value to an array element

Posted 02 October 2012 - 06:46 PM

Well, if that's the case it is easily done by this:

1. Populate array.
2. Copy array values to arraylist.
3. Add values to the 'array' but you're actually adding to the arraylist.
4. Invoke the ArrayList method toArray and return a new array.

^^
Was This Post Helpful? 1
  • +
  • -

#6 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 346
  • Joined: 29-September 12

Re: Add value to an array element

Posted 02 October 2012 - 06:55 PM

Great way, Sprawl! I would love to use that in the future, but too bad my assignment is about making a dynamic array using normal array and increase the size like arraylist. Then we put stuffs in there, and compare its performance with the arraylist version and linkedlist version.
Was This Post Helpful? 0
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Add value to an array element

Posted 02 October 2012 - 07:04 PM

Do you only need to add at the end or should you be able to add anywhere on a dynamic array?
Was This Post Helpful? 1
  • +
  • -

#8 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 346
  • Joined: 29-September 12

Re: Add value to an array element

Posted 02 October 2012 - 07:08 PM

I have 3 other method where I set the value at some index, which I think is easy. I just need to check if the position is within the size of the array, then put the value at that index. Another method is insertAt, which is having a position, and insert the value in the place of the specific index. I think I have to shift the values after that index and increase the size. And do something with the tail elements.
Was This Post Helpful? 0
  • +
  • -

#9 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Add value to an array element

Posted 02 October 2012 - 07:37 PM

Does it matter what type of array it is? I'm worried about primitives, that would be a mess of if statements. Someone knowledgeable in Java, how would primitive types be generalized?

Also, are you permitted to use System.arraycopy? What are the other methods?
Was This Post Helpful? 1
  • +
  • -

#10 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 346
  • Joined: 29-September 12

Re: Add value to an array element

Posted 02 October 2012 - 07:41 PM

The good news is that we only use int[] and Integer, you know for Linklist and ArrayList. and Yes, we can use arraycopy :D
Was This Post Helpful? 0
  • +
  • -

#11 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Add value to an array element

Posted 02 October 2012 - 07:46 PM

Well, I must say yes, it is doable with arraycopy, I've made an implementation myself and it is possible. I think you have to juggle between Integer[] and int through boxing and unboxing, I guess. You can do it! ^^
Was This Post Helpful? 1
  • +
  • -

#12 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 346
  • Joined: 29-September 12

Re: Add value to an array element

Posted 02 October 2012 - 07:50 PM

Yea, I will try to do it now, if I in a pinch, you will see my post again :D At least I catching the feel of it ^_^
Was This Post Helpful? 0
  • +
  • -

#13 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Add value to an array element

Posted 02 October 2012 - 07:51 PM

Good luck then, :)
Was This Post Helpful? 1
  • +
  • -

#14 lilVaratep  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 211
  • Joined: 09-October 11

Re: Add value to an array element

Posted 02 October 2012 - 08:02 PM

View PostRozenKristal, on 02 October 2012 - 06:55 PM, said:

Great way, Sprawl! I would love to use that in the future, but too bad my assignment is about making a dynamic array using normal array and increase the size like arraylist. Then we put stuffs in there, and compare its performance with the arraylist version and linkedlist version.


You're looking for a vector. http://docs.oracle.c...til/Vector.html
Or, make your own Vector class with your own methods and whatnot.
Was This Post Helpful? 1
  • +
  • -

#15 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 346
  • Joined: 29-September 12

Re: Add value to an array element

Posted 02 October 2012 - 08:04 PM

Great info lilVaratep, I would look at it and check it out, definitely!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1