andrej
andrej

Reputation: 587

Java get array in List

I am posting what I think is relevant code. In table class I have :

class Table extends AbstractTableModel {
   private List<String> columnHeaders;
   private List<Object> tableData;
   public Table(SortedSet<String> oznake, List<Object> aRows) {
      columnHeaders= new ArrayList<String>(oznake);
      tableData= new ArrayList<Object>(aRows);
      System.out.println("       tableData:" + tableData.size() + " "+ tableData);
   }

   public int getColumnCount() {
      return columnHeaders.size();
   }
   public int getRowCount() {
      return tableData.size();
   }

   public Object getValueAt(int row, int column) {
      List rowData = (List)(tableData.get(row));
      return (String)rowData.get(column);
   }

   public String getColumnName(int column) {
      return (String)(columnHeaders.get(column));
   }
}

In XmlRead I have:

public class XmlRead {
   public List<Object> getTable() {
      Map<String, String> rowMap = new LinkedHashMap<>();
      List<Object> aRows = new ArrayList<>();
                  rowMap.put(sOznaka, parser.getText());
                  aRows.add(Arrays.toString(rowMap.values().toArray(new String[rowMap.size()])));
      return (List<Object>)aRows;
   }
}

I get from table class:

tableData:3 [[2007-01-01, 27.485, 156.93, 0, 1.3170], [2019-05-06, 25.715, 0, 124.13, 1.1199], [2019-05-09, 25.718, 122.91, 0, 1.1193]]

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.List (java.lang.String and java.util.List are in module java.base of loader 'bootstrap')

    at irose.IroseTable.getValueAt(IroseTable.java:71)

It looks as the problem is the line:

List rowData = (List)(tableData.get(row));

I can not figure it out why. Seems I have to get Objects out of List that have 3 Array.

[[2007-01-01, 27.485, 156.93, 0, 1.3170], [2019-05-06, 25.715, 0, 124.13, 1.1199], [2019-05-09, 25.718, 122.91, 0, 1.1193]]

How can I get Object out of that Array, that is, if I want just:

2007-01-01

How can I do that?

Upvotes: 0

Views: 79

Answers (1)

EaglesMaster
EaglesMaster

Reputation: 41

The ClassCastException means the object you retrieve from tableData.get(row) is of type String, and cannot be casted to a List object. You have 2 options to correct your problem:

-you can keep the String from tableData.get(row) and use this regular expression to retrieve the date ([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))

-or you can change the object contained inside the tableData, by changing the line: aRows.add(Arrays.toString(rowMap.values().toArray(new String[rowMap.size()]))); to

aRows.addAll(rowMap.values().toArray(new String[rowMap.size()]));

Upvotes: 1

Related Questions