1

I have an arrayList which contains Objects. Each object contains a number of strings. I am trying to take these strings and add them to a two dimensional array.

public void iterateRow(Row row)
{ 
    int x = 0;
    int y = size();
    tableArray = new String[y][5];
    while(x < y){
       int z = 0;
       for (String s: row.rowString()){
          tableArray[x][z] = s;
          z++;
       }
       x++;
    }
}

Whenever i run and create a new instance for the row class the method should add the strings contained in Row to the array. However it duplicates the latest entry x times (where x is the total number of entries).

Here is the Row class for further reference:

public class Row
{   
public String appNumber;
public String name;
public String date;
public String fileLoc;
public String country;
public String elementString;
public String results[];

public Row(String appNumber, String name, String date, String fileLoc, String country, Table table)
{
    this.appNumber = appNumber;
    this.name = name;
    this.date = date;
    this.fileLoc = fileLoc;
    this.country = country;
    table.addApplicant(this);
}

public String[] rowString()
{
    String[] a = {appNumber, name, date, fileLoc, country};
    return a;
}}

I think it is a silly logical error in the iterateRow() method but i can't seem to work out what. Any help would be appreciated.

Edit: After everybody's help i have removed the while loop. However it still seems to be duplicating the Row rather than moving onto the next?

public void iterateRow(Row row)
{ int x = 0;
    int y = size();
    tableArray = new String[y][row.rowString().length];
    for(int i =0; i<y;i++){
    int z = 0;
    for (String s: row.rowString()){
       tableArray[x][z] = s;
       z++;
    }x++;}
} 
6
  • What does size() return? Because you're adding the same row rowString() elements to your array "size()" times. Commented Mar 12, 2013 at 18:01
  • Based on your loop, since the parameter 'row' is never changing, your source for building the array will be from this 'row'. I would imagine that after your 'for each' loop you want to go to the next row?... also, your while loop could be simplified as a 'for' loop. Commented Mar 12, 2013 at 18:03
  • Why 5 here, tableArray = new String[y][5] ? Commented Mar 12, 2013 at 18:04
  • Because there are 5 String fields in Row. Commented Mar 12, 2013 at 18:04
  • @Machinegon good shout. I've changed that to tableArray = new String[y][row.rowString().length]; Commented Mar 12, 2013 at 18:08

5 Answers 5

2

The problem looks like it is in the while loop. If size() returns 3, for example, then the while loop will execute with x=0,1,2 so you'll assign tableArray[0], then tableArray[1] and then tableArray[2].

Its hard to tell what the solution is as I can't understand why you've got the while loop in the code at all.

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

Comments

1

It looks like you want to iterate through each element of a List of Rows

Maybe you want to use a method similar to the following:

public void iterateRows(List<Row> rows) {
    int cols = 5;
    int row = 0, col = 0;
    tableArray = new String[rows.size()][cols];
    for(Row row : rows) {
        col = 0;
        for(String c : row.rowString()) {
            tableArray[row][col] = c;
            col++;
        }
        row++:
    }
}

However, it would be good to implement your own error checking incase there are more than 5 columns...

EDIT

This is probably not the best design to use for your program, and I would recommend changing it so that when a new row is added, you don't have to iterate through each of the old rows as well.

Comments

0

Your iterateRow method re-creates tableArray each time it is called. Because of the while loop, your Row object gets replicated y times in the array.

I suspect that you want to create your array outside of the iterateRow method and not use a while loop (just the for loop) to populate the next slot in your array.

1 Comment

It does both, each time it's called. It re-creates the array with tableArray = new String[y][5]; and the while loop repeats the rows.
0

You are not taking care to size the two dimensional array relative to the size of the Row.

While this might not be the smoking gun, it would be far safer to do

 tableArray = new String[y][row.rowString().length];

instead of

 tableArray = new String[y][5];

1 Comment

Thanks for that, will come in handy when more elements are added to the Row class in the future :)
0

I don't know if this is for practical purpose or how IterateRow() is called, but a logical way to get this working would be to declare the array first, add 2 argument in IterateRow(reference of the array and currentRow). Then, inside iterateRow you get rid of the while loop and you just loop for the string proprieties.

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.