Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > LinkedList vs ArrayList

Reply
Thread Tools

LinkedList vs ArrayList

 
 
Robert Klemme
Guest
Posts: n/a
 
      04-12-2006
Vanessa Berni wrote:
> I've been thinking about all the operation I have to do with my set andf
> I've found out that
>
> 1) I've to read all the elements (in order) : I can do it with a
> listiterator
> 2) Given the current element (pointed by the listIterator) (i-th element) I
> have to find the previous element (that is NOT necessarly (i-1))
> 3) Given the current element (pointed by the listIterator) (i-th element) I
> have to find the next element (that is NOT necessarly (i+1))
> 4) I've to do some operation with the 3 elements (modify the current
> element)
> 5) I've to move "up" or "down" the current element


Maybe you should reveal more information how you find previous and next
elements. The best solution might be to create a temporary data
structure (maybe using a Map, SortedMap, SortedSet...), work on that and
create a new List after processing. Note also that you will run into
ConcurrentModificationException if you move elements around the list
while iterating with other methods than the ones provided by ListIterator.

Another thought: from what I read so far you have actually two orders:
one in the list and another one that defines "previous" and "next"
elements. Maybe that's another hint for finding a proper solution.

Kind regards

robert

 
Reply With Quote
 
 
 
 
Vanessa Berni
Guest
Posts: n/a
 
      04-12-2006
"Robert Klemme" <> ha scritto nel messaggio
news:...
> Vanessa Berni wrote:
>> I've been thinking about all the operation I have to do with my set andf
>> I've found out that
>>
>> 1) I've to read all the elements (in order) : I can do it with a
>> listiterator
>> 2) Given the current element (pointed by the listIterator) (i-th element)
>> I have to find the previous element (that is NOT necessarly (i-1))
>> 3) Given the current element (pointed by the listIterator) (i-th element)
>> I have to find the next element (that is NOT necessarly (i+1))
>> 4) I've to do some operation with the 3 elements (modify the current
>> element)
>> 5) I've to move "up" or "down" the current element

>
> Maybe you should reveal more information how you find previous and next
> elements. The best solution might be to create a temporary data structure
> (maybe using a Map, SortedMap, SortedSet...), work on that and create a
> new List after processing. Note also that you will run into
> ConcurrentModificationException if you move elements around the list while
> iterating with other methods than the ones provided by ListIterator.
>
> Another thought: from what I read so far you have actually two orders: one
> in the list and another one that defines "previous" and "next" elements.
> Maybe that's another hint for finding a proper solution.
>
> Kind regards
>
> robert
>


My set is composed of objects that store some values;
the two involved in ordering the set are
int A
int B

They are order using A (without caring about B)
Given an element the previous is the
- one having A less then the A value of current element
- having the same B value

Given an element the next is the
- one having A greater then the A value of current element
- having the same B value

for example
[0, 0], [5,1], [7,0], [9,1], [12,0] , [12,1]
^
| current

What I have to do is:

a) given current (example [7,0])
b) find previous is [0,0]
c) find next is [9, 0]
d) modify in "same way" the A value of current object using prec and next
(example [7,0]-->[10,.0])
e) move modified element in the new correct position
obtaining the new list
[0, 0], [5,1], [9,1], [10,0], [12,0] , [12,1]
^
| "new current"

A way is implement a list

class MyListObject {
MyObject obj;
ListObject next;
ListObject prev;
}

and then "use" next and "prev" to move up and down.

The question is: is there a way of doing the same thing using Java classes?

Thanks a lot again
Vanessa



 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-12-2006
Vanessa Berni wrote:
> "Robert Klemme" <> ha scritto nel messaggio
> news:...
>> Vanessa Berni wrote:
>>> I've been thinking about all the operation I have to do with my set andf
>>> I've found out that
>>>
>>> 1) I've to read all the elements (in order) : I can do it with a
>>> listiterator
>>> 2) Given the current element (pointed by the listIterator) (i-th element)
>>> I have to find the previous element (that is NOT necessarly (i-1))
>>> 3) Given the current element (pointed by the listIterator) (i-th element)
>>> I have to find the next element (that is NOT necessarly (i+1))
>>> 4) I've to do some operation with the 3 elements (modify the current
>>> element)
>>> 5) I've to move "up" or "down" the current element

>> Maybe you should reveal more information how you find previous and next
>> elements. The best solution might be to create a temporary data structure
>> (maybe using a Map, SortedMap, SortedSet...), work on that and create a
>> new List after processing. Note also that you will run into
>> ConcurrentModificationException if you move elements around the list while
>> iterating with other methods than the ones provided by ListIterator.
>>
>> Another thought: from what I read so far you have actually two orders: one
>> in the list and another one that defines "previous" and "next" elements.
>> Maybe that's another hint for finding a proper solution.
>>
>> Kind regards
>>
>> robert
>>

>
> My set is composed of objects that store some values;
> the two involved in ordering the set are
> int A
> int B
>
> They are order using A (without caring about B)
> Given an element the previous is the
> - one having A less then the A value of current element
> - having the same B value
>
> Given an element the next is the
> - one having A greater then the A value of current element
> - having the same B value
>
> for example
> [0, 0], [5,1], [7,0], [9,1], [12,0] , [12,1]
> ^
> | current
>
> What I have to do is:
>
> a) given current (example [7,0])
> b) find previous is [0,0]
> c) find next is [9, 0]
> d) modify in "same way" the A value of current object using prec and next
> (example [7,0]-->[10,.0])
> e) move modified element in the new correct position
> obtaining the new list
> [0, 0], [5,1], [9,1], [10,0], [12,0] , [12,1]
> ^
> | "new current"
>
> A way is implement a list
>
> class MyListObject {
> MyObject obj;
> ListObject next;
> ListObject prev;
> }
>
> and then "use" next and "prev" to move up and down.
>
> The question is: is there a way of doing the same thing using Java classes?


Yes, of course. Here's one idea sketched: use three collections

the input SortedSet ordered by a
a temp SortedMap with b as key
an output SortedSet ordered by a

Before iterating create the temp map (by iterating of course)

Iterate and the input set, do adjustments as described above and insert
the current instance into the output set.

....

Kind regards

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare LinkedList with ArrayList and Vector Amit Jain Java 8 10-03-2007 03:24 AM
Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList? xz Java 16 08-04-2007 10:33 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57