3

I am automating my gui app. I am fetching the data from database here data contains 6 player and their position..and now I need to store all the data into arraylist... I need to store all the data and put into arraylist..

java.sql.Statement stm1 = con.createStatement();
     java.sql.ResultSet result1 = stm1.executeQuery(player_name_pos_query);//lineup query

     List<String> al1=new ArrayList<String>();
         while(result1.next())
         {
                // System.out.println(2);
                 lineup_player_name=result1.getString(1);
                 pos=result1.getString(2);

                 al1.add(lineup_player_name);
                 al1.add(pos);



        System.out.println(al1);//  [Mahmood, F]




         }

It is printing only last values...I want to take all values and keep it in single array

[Jermaine O'Neal, G]

How to keep it in one arrayLIST [Mohamood, F,Josh Childress, C, Corsley Edwards, G, Robert Hite, F,Nate Robinson, C, Jermaine O'Neal, G] so I can take size of 12 and like this 0 to 12...And I can get each one by one string Like get(0) --Mohammed, get(1)--F, get(2)--Josh Childress... Then only I can able to click the Gui for respective player position and my loop condition would work..

2 Answers2

1

I would approach with a dedicated class for keeping Name-Position mapping:

public class NamePosition {

    String name;
    String position;

    public NamePosition(String name, String position) {
        this.name = name;
        this.position = position;
    }

    public String getName() {
        return name;
    }

    public String getPosition() {
        return position;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NamePosition that = (NamePosition) o;
        return Objects.equals(name, that.name) &&
                Objects.equals(position, that.position);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, position);
    }

    @Override
    public String toString() {
        return "NamePosition{" +
                "name='" + name + '\'' +
                ", position='" + position + '\'' +
                '}';
    }
}

Then you will use this data structure like this:

public static void main(String[] args) {

    List<NamePosition> namePosList = new ArrayList<>();

    namePosList.add(new NamePosition("Mohamood", "F"));             //
    namePosList.add(new NamePosition("Josh Childress", "C"));       //  In your case you will fill the values from your ResultSet
    namePosList.add(new NamePosition("Corsley Edwards", "G"));      //  and then be able to access the name andposition using getters
    namePosList.add(new NamePosition("Robert Hite", "F"));          //

    System.out.println(namePosList.toString()); // ** see result under the code snippet

    for(NamePosition namePos: namePosList){
        String currentName = namePos.getName();
        String currentPosition = namePos.getPosition();
        //
        //  TODO: Process the name and position here
        //
    }
}

** - below is the output of println method from the code:

[NamePosition{name='Mohamood', position='F'}, NamePosition{name='Josh Childress', position='C'}, NamePosition{name='Corsley Edwards', position='G'}, NamePosition{name='Robert Hite', position='F'}]

So you just need to amend my code to fill up the list from your database response instead of my example where I hardcode the values.

Alexey R.
  • 11,590
  • 5
  • 19
  • 39
0

I would suggest using HashMap instead of ArrayList here as we have keys(PlayerName) and values(Position) both in the record to store.

HashMap Usage Example:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
....

Map<String,String> mMap = new HashMap<String, String>();
mMap.put("key", "something");
Iterator iter = mMap.entrySet().iterator();
while (iter.hasNext()) {
    Map.Entry mEntry = (Map.Entry) iter.next();
    System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
}
Vishal Aggarwal
  • 5,572
  • 2
  • 19
  • 36