• Post Reply Bookmark Topic Watch Topic
  • New Topic

Matrix LL representation  RSS feed

 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I am trying to at this point read in a matrix from a txt file in the format with order of the matrix leading the matrix. Like this :
1
5
2
2 3
5 9
3
3 -2 4
-1 5 2
-3 6 4
4
2 4 5 6
0 3 6 9
0 0 9 8
0 0 0 5
4
2 4 5 6
0 0 0 0
0 0 9 8
0 0 0 5


My code then is supposed to read it in and store it as an Array of singly linked lists. I am having trouble with my code, I am only getting outputs where it is only storing the first line of the txt matrixs like so:

5.0

2.0 3.0
2.0 3.0

3.0 -2.0 4.0
3.0 -2.0 4.0
3.0 -2.0 4.0

2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0

2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0
2.0 4.0 5.0 6.0


Is there something obvious in my code that is making it only save the first lines and how can i fix it. Thanks!

And here is my Test class



Here is my code for the LL implementation


 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just from looking at the code. I did not check whether I am right:

The problem is your temp LinkedList. You add values to it in the loop and assign this temporary list to data but you never clear or reassign it.
So when you iterate from 0 to order you always see elements added in first iteration of the loop.
 
paul spriesterbach
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Just from looking at the code. I did not check whether I am right:

The problem is your temp LinkedList. You add values to it in the loop and assign this temporary list to data but you never clear or reassign it.
So when you iterate from 0 to order you always see elements added in first iteration of the loop.


so when I add a clear (set first node to null) to the inside the i through order loop it just clears out the linked list for each part in the array after I assign it to the array
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the assignment does not do what you think it does.
It does not copy the data from one list to another. It literally assigns a reference to the same temp list to every entry in your data table.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!