LinkedList vs ArrayList

R

Robert Klemme

Vanessa said:
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
 
V

Vanessa Berni

Robert Klemme said:
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
 
R

Robert Klemme

Vanessa said:
Robert Klemme said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top