• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java arraylists search and replace elements in each list  RSS feed

 
Kovacs Akos
Ranch Hand
Posts: 87
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have lists in an arraylist(2 dimensional), elements are recurrent in lists. I would like to search for a specified element and replace the second element after it in all the lists.



Example - I search for 154 and 358, so zeros should be changed to 1:

[288, 362, 0, 365, 85, 0, 137, 10, 0, 154, 358, 1]

[285, 226, 0, 137, 10, 0, 20, 30, 1, 387, 297, 0, 154, 358, 1]


This code do it only for one match, not for all matches.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry didn't understand well. Could you please explain more and also how do you define "lists in an arraylist(2 dimensional)" ?
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a "big" list, which each elements are another lists.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so you are using List of list.

Also did not understand why in second for loop you are incrementing by 3? What is 'h' in if condition?

So it is better if you could explain little bit more.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In every iteration are number, number+1, number +2 indexes. Incrementing +3 should make it possible not to examine 1 number twice.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Kovacs,

Is this still part of your work on adjacency lists?
If so, why dont you use Points? That will make your code
really very much easier to make and understand. I refer you to
this topic:

http://www.coderanch.com/t/647964/java/java/Arraylists-arraylist-representation-relationships

If I look at these topics:

http://www.coderanch.com/t/649061/java/java/Special-array-pop

and

http://www.coderanch.com/t/648997/java/java/Adjacency-lists-Java

I think the code as presented there is quite hard to follow, and therefore
quite hard to debug.

You started with a list containing all the edges, in textform:
x1 250 y1 300 x2 0 y2 50
(...)

So, lets say you were able to create, from your initial list, the
HashMap<Point, ArrayList<Point>>.

Can you tell us what it is that you are trying to achieve? Things
that come to my mind are:

1) finding out if there is a path between Points p1 and p2
2) if so, find the shortest path
3) how many times is Point p the second point of an edge? (this frequency
has a name, but I forgot)
4) create an adjacency matrix from the map

et cetera.

So, what are you trying to achieve here?

Greetz,
Piet

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!