1

I have a String array defined and read some values from a table into it:

String[][] ssRet = null;
ssRet = new String[tblRoDe.getNumRows()+1][2];
ssRet[0][0] = "AGR_NAME";
ssRet[0][1] = "TEXT";
for(int j=0; j<tblRoDe.getNumRows(); j++ )
{
    tblRoDe.setRow( j );
    ssRet[j+1][0] = tblRoDe.getString( "AGR_NAME"   );
    ssRet[j+1][1] = tblRoDe.getString( "TEXT"       );
    logger.debug("------- Start Entry #" + (j+1) + " -------");
    logger.debug(ssRet[0][0] + ": " + ssRet[j+1][0]);
    logger.debug(ssRet[0][1] + ": " + ssRet[j+1][1]);
    logger.debug("------- End Entry #" + (j+1) + " -------");
}

My task now is: depending on a function param I will have to read values from a different table into the same string array (append this). There are still only two columns but I have to add more lines.

How can I achieve that?

1
  • 2
    you can´t change the original size of an array(only by reinitializing it you could change it but that would make it a new array). You have to use a List instead. Commented Nov 11, 2015 at 7:55

4 Answers 4

2

You will have problems when you stick with arrays, as they are fix in their size.

For a more dynamic solution you will need a Collection, such as List - this grow with the data.

Sign up to request clarification or add additional context in comments.

Comments

2

Arrays cannot be expanded once created. So, there are the following solutions.

  1. Create array of needed size. You can achieve this if you know the array size before reading the data.
  2. create new array once you have to "expand" it and copy data from the small array to the bigger one. Then continue to fill the bigger array. You can use System.arraycopy() or implement the logic yourself
  3. Use any implementation of java.util.List instead of array.

BTW unless this is a school exercise avoid using 2 dimensional array in this case. I'd recommend you to define your custom class with 2 fields name and text and then use 1 dimensional array or list according to your choice.

Comments

1

If you have a different amount of Rows, and you can't excatly say how much rows it will be you should use ArrayList instead of an array with an fixed size. To an ArrayList you can add as muchs values as you want. The Problem is, that an Array, once it is created, cant be resized.

To this List you could save Objects of your own type(to handle your data) like this:

    public class TableRow {

    private String agr_name;
    private String text;
    private int entryNr;

    public TableRow(String agr_name, String text, int entryNr) {
        this.agr_name = agr_name;
        this.text = text;
        this.entryNr = entryNr;
    }

    public String getAgr_name() {
        return agr_name;
    }

    public void setAgr_name(String agr_name) {
        this.agr_name = agr_name;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getEntryNr() {
        return entryNr;
    }

    public void setEntryNr(int entryNr) {
        this.entryNr = entryNr;
    }

}

This used to your code should look like that:

ArrayList<TableRow> allRows = new ArrayList<>();
        for (int j = 0; j < tblRoDe.getNumRows(); j++) {
            tblRoDe.setRow(j);
            TableRow trow = new TableRow(tblRoDe.getString("AGR_NAME"), tblRoDe.getString("TEXT"), j);
            allRows.add(trow);
            logger.debug("------- Start Entry #" + trow.getEntryNr() + " -------");
            logger.debug("AGR_Name: " + trow.getAgr_name());
            logger.debug("TEXT: " + trow.getText());
            logger.debug("------- End Entry #" + trow.getEntryNr() + " -------");
        }

Hope that helps!

1 Comment

Thanks a lot for this. I will go ahead with this solution
1

You can't manipulate the array size after creating it , so Use List as it can grow and it's considered as a dynamic solution

List<List<String>> ssRet = new ArrayList<ArrayList<String>>();
List<String> temp=ArrayList<String>(); //creating temp list
temp.add("AGR_NAME");
temp.add("TEXT");
ssRet.add(temp);
for(int j=0; j<tblRoDe.getNumRows(); j++ )
{
    tblRoDe.setRow( j );
    temp.clear();//clear list instead of creating new one
    temp.add(tblRoDe.getString("AGR_NAME"));
    temp.add(tblRoDe.getString("TEXT"));
    ssRet.add(temp);
}

To get the data :-

for(int i = 0 ; i < ssRet.size() ; i++){
   for(int j = 0 ; j < ssRet.get(i).size() ; j++){
      System.out.println("The "+(i+1) + " array data");
      System.out.println(ssRet.get(i).get(j));
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.