0

I have the following method:

   public Object[][] getTableData(JTable table) {
    DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    int nRow = dtm.getRowCount(), nCol = 3;
    Object[][] tableData = new Object[nRow][nCol];
    for (int i = 0; i < nRow; i++) {
        tableData[i][0] = dtm.getValueAt(i, 5);
        //System.out.print(tableData[i][0] + " ");
        tableData[i][1] = dtm.getValueAt(i, 4);
        //System.out.print(tableData[i][1] + " ");
        tableData[i][2] = (dtm.getValueAt(i, 2)) + " " + `(dtm.getValueAt(i, 3));`
        //System.out.print(tableData[i][2] + " ");
        //System.out.println();
    }
    return tableData;
}

I also have the following method:

    public Object[] head(JTable table) {
        JTableHeader th = table.getTableHeader();
        TableColumnModel tcm = th.getColumnModel();
        Object[] headdata = new Object[3];

        headdata[0] = tcm.getColumn(5).getHeaderValue();
        headdata[1] = tcm.getColumn(4).getHeaderValue();
        headdata[2] = tcm.getColumn(2).getHeaderValue();
        //System.out.println(headdata[x]);
        list = new ArrayList<>(Arrays.asList(headdata));//list refers to column names
//System.out.println(list.get(0) +" "+ list.get(1) +" " +  list.get(2));
        return headdata;
    }

I want to combined the 2 methods into a single method however one return type is Object[] and another return type is Object[][]

How could i combine the methods so i bring back 2 return types and bring back return tableData and return headdata from the same method.

I am 2 years amateur experienced in Java.

Ingram
  • 654
  • 2
  • 7
  • 29
  • Create a type that encapsulates both, and return an instance of it. – Oliver Charlesworth May 02 '16 at 10:03
  • Why use `Object[]` when you can create data classes that would hold your data? – rapasoft May 02 '16 at 10:03
  • @ Oliver Charlesworth i am interested in how i would do that..i was thinking something like public Object[][] getTableData(JTable table),Object[] head(JTable table) { .....but im not sure what the syntax for this would be like? – Ingram May 02 '16 at 10:21
  • @Oliver Charlesworth i dont suppose you could show me an example of this with the methods i have attached - it would be really beneficial – Ingram May 02 '16 at 18:46

0 Answers0