array[index]= value;?? I dont need to loop right? What kind of data structure need a loop to operate?
Add value to an array element
Page 1 of 114 Replies - 197 Views - Last Post: 02 October 2012 - 08:04 PM
#1
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:
Replies To: Add value to an array element
#2
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.
#3
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.
#4
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.
#5
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.
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.
#6
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.
#7
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?
#8
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.
#9
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?
Also, are you permitted to use System.arraycopy? What are the other methods?
#10
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
#11
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!
#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
At least I catching the feel of it
#13
Re: Add value to an array element
Posted 02 October 2012 - 07:51 PM
Good luck then,
#14
Re: Add value to an array element
Posted 02 October 2012 - 08:02 PM
RozenKristal, 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.
#15
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!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|