Thanassis
Thanassis

Reputation: 877

How do I use multidimensional arrays with array list?

I want to add some records from sql query but the output is not correct. Always return the last record.

The correct list is :

John

Nick

Mary

Joe

,but always return Joe.

This is the method to add the elements:

public ArrayList<String[][]> getFiledArrayList()
{
   // ArrayList<String[][]> fieldsList = new  ArrayList<>();
    String[][] tempRow = new String[1][2];
    ResultSet result;

    String sql = "select id, name_of from field";
    result = database.exeQueryStatement(sql);
    try 
    {
        while(result.next())
        {
            tempRow[0][0] = result.getString("id");
           // System.out.println(tempRow[0][0]);
            tempRow[0][1] = result.getString("name_of");
           // System.out.println(tempRow[0][1]);
            fieldsList.add(tempRow);
            System.out.println(fieldsList.get(0)[0][1]);
        }
    } 
    catch (SQLException ex) 
    {
        Logger.getLogger(FieldManage.class.getName()).log(Level.SEVERE, null, ex);
    }

    return fieldsList;

I put the id and the name_of in a String[1][2] table and I want to show the name_of in a jComboBox. Ι want to make an insert and watch the name_of with id

FieldManage fieldmanage = new FieldManage();
   ArrayList<String[][]> listOfField;
   listOfField = fieldmanage.getFiledArrayList();
   String[] fields = new String[listOfField.size()];
   System.out.println(listOfField.get(0)[0][0]);
   for (int i=0; i<listOfField.size(); i++)
   {
        fields[i] = listOfField.get(i)[0][1];
        System.out.println(fields[i]);//test print show always joe!
    }
     jComboFields.setModel(new javax.swing.DefaultComboBoxModel(fields));

This code always return Joe.

Also I want to know if there is better way to match an jcombo element with an id.

Upvotes: 1

Views: 175

Answers (3)

deleted_user
deleted_user

Reputation: 3805

You are trying to create an array of object values.

Using ArrayList<String[][]> is not the way to do this.

Create a class

public class Person {
    private long id;
    private String name;

    public long getId() {
         return id;
    }

    public void setId(long id) {
        self.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        self.name = name;
    }

}

Then in your code....

ArrayList<Person> myPeople = new ArrayList<Person>();

Person p = new Person();
p.setName("mary");
p.setId(1);

myPeople.add(p);

Start from there, your doing it the hard way, and given that you are having problems understanding arrays and object references, learn the language before you start using multidimensional primitive arrays in conjunction with loops and collections.

Upvotes: 2

Azodious
Azodious

Reputation: 13872

Move this line:

String[][] tempRow = new String[1][2];

as the first line in your while(result.next()) loop.

What is happening:

if you put tempRow outside loop, in 2nd iterator on loop same array is modified i.e overwritten by next value. At the completion of while loop, your fieldsList contains the last element only at all indexes.

Run your loop 3 times and you'' see Mary as output.

Upvotes: 1

NPE
NPE

Reputation: 500495

When populating fieldsList, you repeatedly add references to the same object (tempRow). When your loop modifies the contents of tempRow, all previously added entries also change (since they're the same object).

Move the following line inside the loop:

String[][] tempRow = new String[1][2];

Upvotes: 6

Related Questions