• Post Reply Bookmark Topic Watch Topic
  • New Topic

This method takes a "List of List" as an argument. Why are these ever used?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the following method:



This method has a return type of "List" of type "AllotmentData." I understand this much.
However, the arguments which it takes are confusing to me, mainly because I have never seen a construct like this.

It is taking "fileContents" which will be a "List" of type String... but wait... it's embedded within another List.
I think it's called a "List of List" which I can't seem to wrap my head around...

If it's at all relevant, this method parses CSV data...

When are Lists of Lists useful?
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:
If it's at all relevant, this method parses CSV data...

Well, since you put it that way, the outer list is probably a list of lines read in and each line represents a list of fields. So in more abstract terms, you have a List<Line> where a Line is a List<FieldValue> and a FieldValue is a String. So, working backwards from there and substituting terms,

Given, FieldValue => String  (a FieldValue is a String)
Therefore, List<FieldValue> => List<String>

Given, Line => List<FieldValue>
Therefore, List<Line> => List<List<FieldValue>>
It follows that List<Line> => List<List<String>>

Does that make more sense?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A List of Lists is the object oriented approach to get something similar to a 2D array (note that Java doesn't have 2D arrays per se, it has arrays of arrays).
So a  List<List<String>> listVar is similar to String[][] arrayVar.
To get at an array element you'd use
to get to an list element you'd use
The first listVar.get(2) returns a List<String>, and when applying get(4) to that List you get a String.

Does this make any sense or did I make it worse?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another example: Given a CSV text file

You could parse that into a list of lines and parse the lines into a  list of fields.
To display them all

Or, to get to the 2nd line, 4th String
listVar.get(1).get(3) would return the String "denver".
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Real world analogy:

  • Every week you make a list of groceries that you need on a sheet of paper.
  • After a number of weeks have gone by, you have a bunch of sheets of paper, each with a list.
  • Staple the sheets together. You now have a list of lists.
  •  
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!