2

I'm having some trouble iterating through ArrayList. I have a class named Row which extends ArrayList<String>. I have another class named Table which extends ArrayList<Row>.

I am trying to iterate through the Table class to convert the ArrayList of Rows to a two-dimensional array of Strings.

Here is my code for the Table class:

public class Table extends ArrayList<Row>
{
public Row[] appArray; //Array of single applicant details
public String tableArray[][]; //Array of every applicant
private ArrayList<Row> ar;
private Row r;

public Table()
{
}

public void addApplicant(Row app)
{
    add(app);
    displayable();
}

public void convertToArray()
{
    int x = size();
    appArray=toArray(new Row[x]);
}

public void displayable()
{
int i,j;
for (Row r: ar)
   i=0;
    for(String s: r){
        j=0;
        tableArray[i][j]=s;
        j++;
    }

}}

and here is the Row class:

public class Row extends ArrayList<String>
{
public Row(String appNumber, String name, String date, String fileLoc, String country, Table table)
{
    addDetails(appNumber,name,date,fileLoc,country);
    table.addApplicant(this);
}

public void addDetails(String appNumber, String name, String date, String fileLoc, String country)
{
    add(appNumber);
    add(name);
    add(date);
    add(fileLoc);
    add(country);
}}

The method i am having trouble with is displayable() in the Table class. It tells me i may not have been initialized However if I initialize it in the second for each loop it will only iterate through the first element in my Table ArrayList?

Thanks in advance for any pointers.

2
  • Setting i and j to 0 inside their respective for loops seems like a possible logic error to me. Commented Mar 11, 2013 at 15:39
  • 1
    There's no such thing as an ArrayList<Object<String>>, because class Object does not have type parameters (Object<String> is invalid). Commented Mar 11, 2013 at 15:40

3 Answers 3

4

You just missed block in for (Row r: ar) loop.

Also, as Fortega noted, you never increment i in your code, and I suppose that you zero your counters in wrong place.

So, instead of:

for (Row r: ar)
   i=0;
   for(String s: r){
     j=0;
     tableArray[i][j]=s;
     j++;
   }

You should write something like:

i=0;
for (Row r: ar) {
  j=0;
  for(String s: r){
    tableArray[i][j]=s;
    j++;
  }
  i++;
}

Or better to narrow down visibility area of counters and join declarations with initializations:

// Remove previous declarations

int i=0;
for (Row r: ar) {
  int j=0;
  for(String s: r){
    tableArray[i][j]=s;
    j++;
  }
  i++;
}
Sign up to request clarification or add additional context in comments.

Comments

3

The displayable method has two errors:

  • the first for-loop does not have {}
  • i is not incremented (i++; missing)

code:

public void displayable()
{
  int i,j;
  for (Row r: ar){
    i=0;
    for(String s: r){
        j=0;
        tableArray[i][j]=s;
        j++;
    }
    i++;
  }
}

2 Comments

I think what such increment isn't something desired, as each of counters becomes zero again on next iteration. So there is also need to move i=0 and j=0 one level up
It works! Thanks for all the help guys. Silly mistakes can be such a hassle. Thanks again.
2

Change this:

for (Row r : ar)
            i = 0;
        for (String s : r) {
            j = 0;
            tableArray[i][j] = s;
            j++;
        }

to:

for (Row r : ar) {
            i = 0;
            for (String s : r) {
                j = 0;
                tableArray[i][j] = s;
                j++;
            }
            i++;
        }

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.