Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Matrix LL representation

 
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


 
Bartender
Posts: 2219
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • 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: 2219
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!