0

I know this question is kind of repetitive But still I can't get an answer.
I have 2 arraylists containing name and description respectively as

    final ArrayList<String> arrayOne = new ArrayList<String>(); // containing names
    final ArrayList<String> arraytwo = new ArrayList<String>(); // containing description

I need a view like

enter image description here

I have tried

arraytwo.add(arrayOne); 

&

arrayThree.addAll(arrayOne);
arrayThree.addAll(arrayTwo);

But can't a desired arraylist.
Regards

10
  • Are the size of arrayone and arraytwo same? Commented May 30, 2016 at 9:21
  • 1
    create a POJO with two member variables name and description and then create a custom ArrayList<YourPojo> it would be easy to manage that way, Commented May 30, 2016 at 9:21
  • they are of same size @NongthonbamTonthoi Commented May 30, 2016 at 9:22
  • Yes i was going to write about POJO Commented May 30, 2016 at 9:22
  • 1
    use hashmap instead of arraylist to merge two arraylist Commented May 30, 2016 at 9:22

2 Answers 2

3

POJO class

public class Model
{
    String name;
    String desc;

    public String getName() {
        return name;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        desc = desc;
    }
    }

For Storing to arraylist

ArrayList<Model> arrayModel = new ArrayList<Model>();

      for(int i=0;i<arrayOne.size();i++)
   {
        Model model=new Model();
        model.setName(arrayOne.get(i));
        model.setDesc(arrayTwo.get(i));
        arrayModel.add(model);
   }
Sign up to request clarification or add additional context in comments.

Comments

2

If you don't want to create POJO, try this:

List<String> nameList;
List<String> desList;

//for storing
Map<String, String> map = new HashMap<>();
for(int i=0;i<nameList.size();i++) {
    map.put(nameList.get(i), desList.get(i));
}

//for retrieving
for(Map.Entry<String, String> m : map.entrySet())
    String nameListItem = m.getKey();
    String desListItem = m.getValue();
}

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.