Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an example that reads a list of CSV strings into a list of lists and then loops through that list of lists and prints the CSV strings back out to the console.</p> <pre><code>import java.util.ArrayList; import java.util.List; public class ListExample { public static void main(final String[] args) { //sample CSV strings...pretend they came from a file String[] csvStrings = new String[] { "abc,def,ghi,jkl,mno", "pqr,stu,vwx,yz", "123,345,678,90" }; List&lt;List&lt;String&gt;&gt; csvList = new ArrayList&lt;List&lt;String&gt;&gt;(); //pretend you're looping through lines in a file here for(String line : csvStrings) { String[] linePieces = line.split(","); List&lt;String&gt; csvPieces = new ArrayList&lt;String&gt;(linePieces.length); for(String piece : linePieces) { csvPieces.add(piece); } csvList.add(csvPieces); } //write the CSV back out to the console for(List&lt;String&gt; csv : csvList) { //dumb logic to place the commas correctly if(!csv.isEmpty()) { System.out.print(csv.get(0)); for(int i=1; i &lt; csv.size(); i++) { System.out.print("," + csv.get(i)); } } System.out.print("\n"); } } } </code></pre> <p>Pretty straightforward I think. Just a couple points to notice:</p> <ol> <li><p>I recommend using "List" instead of "ArrayList" on the left side when creating list objects. It's better to pass around the interface "List" because then if later you need to change to using something like Vector (e.g. you now need synchronized lists), you only need to change the line with the "new" statement. No matter what implementation of list you use, e.g. Vector or ArrayList, you still always just pass around <code>List&lt;String&gt;</code>.</p></li> <li><p>In the ArrayList constructor, you can leave the list empty and it will default to a certain size and then grow dynamically as needed. But if you know how big your list might be, you can sometimes save some performance. For instance, if you knew there were always going to be 500 lines in your file, then you could do: </p></li> </ol> <p><code>List&lt;List&lt;String&gt;&gt; csvList = new ArrayList&lt;List&lt;String&gt;&gt;(500);</code></p> <p>That way you would never waste processing time waiting for your list to grow dynamically grow. This is why I pass "linePieces.length" to the constructor. Not usually a big deal, but helpful sometimes.</p> <p>Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload