0

I'm having some trouble with adding a String in my "Row" class to my "Table" class. So every time i create a class called row it will add the submitted string to the same instance of the class "Table". Here is my Row class:

public class Row extends ArrayList<Table>{
public ArrayList<String> applicant;
public String row;
/**
 * Constructor for objects of class Row
 */
public Row()
{
    applicant = new ArrayList<String>();
    convertToString();
    applicants(row) //<------ This is the arrayList in the Table class that i wish to           add the row to
}

private void convertToString()
{
    for(int i = 0; i<applicant.size(); i++)
        {
            row = applicant.toString();
        }
}
}

Here is my "Row" class:

public class Table {

    public ArrayList<String> applicants;

    public String appArray[];

    /**
     * Constructor for objects of class Table
     */
    public Table() {
        applicants = new ArrayList<String>();
    }

    public void addApplicant(String app) {
        applicants.add(app);
        toArray();
    }

    public void toArray() {
        int x = applicants.size();
        appArray = applicants.toArray(new String[x]);
    }

    public void list() // Lists the arrayList
    {
        for(int i = 0; i < applicants.size(); i++) {
            System.out.println(applicants.get(i));
        }
    }

    public void listArray() // Lists the Array[]
    {
        for(int i = 0; i < appArray.length; i++) {
            System.out.println(appArray[i]);
        }
    }
}

Any help would be really appreciated.

3
  • The way you have your classes named, it looks like a row is a collection of tables. Did you mean to have public class Table extends ArrayList<Row> ? Commented Feb 21, 2013 at 22:14
  • Can't you just leave out the .convertToString() stuff? If you pass in a ArrayList<String> (that you've just created to be empty actually), you don't really need to convert it probably. And also, it seems like you're using applicants as a global in the constructor of Row(), which is unusual. Commented Feb 21, 2013 at 22:17
  • @SamDufel You're right. I want a table to be a collection of rows. I made the change however now. When i compile the method toArray() in the table class gets an error? "toArray() in Table cannot implement toArray() in java.util.List return type void is not compatible with java.lang.Object[]" Commented Feb 21, 2013 at 22:30

1 Answer 1

2

applicants(row) is not a method in the Row/Table class.

If you wish to add the row to the Arrraylist in the Row class then use the add method

applicant.add(row) method.

Also you should note that Table and Row relationship does not require you to create a Row class extending the Table class. It should be two separate classes. So Table and Row class will have a one to Many relationship. So modify the Table class such that it is able to add several instances of Row class.

I dont know what you are trying to do but lets say you a Row class should contain two things a rowID and applicantName. And a table class which will have many rows representing each Applicant.

so Row class will somewhat look like this:

public class Row extends ArrayList<Table>{
String applicantName;
int applicantID;


/**
 * Constructor for objects of class Row
 */
public Row(int appID, String appName)
{
applicantName = appName;
applicantID = appID;

}

public getApplicantName(){
return applicantName;
}

public getApplicantID(){
return applicantID; 
}


}

And the table class will look like this:

public class Table {

    public ArrayList<Row> applicants;

    public String appArray[];

    /**
     * Constructor for objects of class Table
     */
    public Table() {
    applicants = new ArrayList<String>();
    }

    public void addApplicant(Row app) {
    applicants.add(app);

    }


    public void list() // Lists the arrayList
    {
    for(int i = 0; i < applicants.size(); i++) {
        Row row = (Row) applicants.get(i);  
        System.out.println("Applicant ID: "+ row.getApplicantID() +
        "  Applicant Name: " + row.getApplicantName());
    }
    }

}

so use the above classes in below manner:

Row r1 = new Row(1, "Tom");
Row r2 = new Row(2, "Hoggie");
Row r3 = new Row(3, "Julie");

Table table = new Table();

table.addApplicant(r1);
table.addApplicant(r2);
table.addApplicant(r3);

//So now when you will call the below method list it will print all the applicants with //their ids

table.list();
4
  • I want to add row to the arrayList<applicants> in the Table class. Sorry if it looks really unclear. Thanks for the help though. Commented Feb 21, 2013 at 22:27
  • @Hoggie1790 what should a row class contain? One applicant I assume? Commented Feb 21, 2013 at 22:33
  • Yes, class Row should contain only one applicant. Commented Feb 21, 2013 at 22:42
  • I have modified the answer is this what you are trying to achieve? Commented Feb 21, 2013 at 22:54

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.