Search code examples
javaarraylistprintwriter

Accessing Elements in an ArrayList of ArrayLists


I have an ArrayList of ArrayLists, and I'm trying to access the elements inside, and generally add, remove, replace, and get the items.

My code is below.

import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
 

public class MarvelNetworkRearrange {
    public static void main(String[] args) {
        
        BufferedReader csvReader = null;
        
        try {
            String newLine;
         ArrayList B = new ArrayList<ArrayList>();
            csvReader = new BufferedReader(new FileReader("MARVEL P2P.csv"));
         Integer i = 0;
            

            while ((newLine = csvReader.readLine()) != null) 
         {
            B.add(A(newLine));
            }
         File f = new File("MarvelRearranged.csv");
         PrintWriter printWriter = new PrintWriter(f);
         
         ArrayList<String> x = new ArrayList<String>();
         x = B.get(0);
         x.remove(0);
         
         System.out.println(x);
         
         
        } 
      catch (IOException e) 
      {
            e.printStackTrace();
        } 
      finally 
      {
            try 
         {
                if (csvReader != null) csvReader.close();
            } 
         catch (IOException newException) 
         {
                newException.printStackTrace();
            }
        }
    }
    public static ArrayList<String> A(String fromCSV) {
        ArrayList<String> csvResult = new ArrayList<String>();
        
        if (fromCSV != null) 
      {
            String[] splitData = fromCSV.split("\\s*,\\s*");
            for (int i = 0; i < splitData.length; i++) 
         {
                if (!(splitData[i] == null) || !(splitData[i].length() == 0)) 
            {
                    csvResult.add(splitData[i].trim());
                }
            }
        }
        return csvResult;
    }
}

I'm reading in a .csv file, and I'm attempting to rearrange the items in the .csv file in order to create a .csv file that I can use more effectively for a project. However, when I run this code, I get "error: incompatible types: Object cannot be converted to ArrayList" at "x = B.get(0);". I have also tried x.get(0).remove(0).

I am trying to remove the first element of the first ArrayList in the ArrayList B, so what am I doing wrong? The reason I'm trying to remove it is that the specified element is a blank space in the .csv (done intentionally when creating the original, as it was the space between the headers in a matrix).

Please help!


Solution

  • You have defined your ArrayList B without type, but ArrayList x has String type. Either you need to cast the type, or you need to follow below approach which is better:

    Specify type of ArrayList B:

    ArrayList<ArrayList<String>> B = new ArrayList<>();
    

    To delete any element from your ArrayList, skip creating another object and point to an element of B, directly delete from B:

    B.get(0).remove(0);