1

I have something in my mind which have been bothering me for quite a while now and hope I can get some kind souls to help... :D

Anyway, here's what I wanna do:

I do actually have a number of list, say

List<String> a = {"for testA1", "for testA2", "for testA3"};
List<String> b; = {"for testB1", "for testB2", "for testB3"};
List<String> c; = {"for testC1", "for testC2", "for testC3"};

And I have an array list of objects that I want to add these strings into, i.e.

List<ObjectA> thislist = new ArrayList<ObjectA>();

In my ObjectA class I have:

public class ObjectA{

   String testA;
   String testB;
   String testC;
}

My final output should be:

List<ObjectA> - 
  • testA1
  • testB1
  • testC1

  • testA2

  • testB2
  • testC2

  • testA3

  • testB3
  • testC3

How can I go about setting these Strings in my object from the given individual list?

Sorry for my bad English, I hope it's understandable.

I had this in mind but didn't quite understand.

My code was

List<String> aList = ....;
List<String> bList = ....;
List<String> cList = ....;

List<ObjectA> someList = new ArrayList<ObjectA>();

for(String a: aList){
    ObjectA obj = new ObjectA();    
    obj.setTestA(a);                                
}

ListIterator litr = someList.listIterator();
while(litr.hasNext()) {
    ObjectA element = (ObjectA) litr.next();
   // I wanna loop List<String> bList and add element.setTestB but it doesn't work.
   // This is what I wanna do...
}
Community
  • 1
  • 1
prog rice bowl
  • 788
  • 3
  • 19
  • 36

4 Answers4

6
Iterator<String> aIt = a.iterator();
Iterator<String> bIt = b.iterator();
Iterator<String> cIt = c.iterator();

List<ObjectA> thislist = new ArrayList<ObjectA>();


// assumes all the lists have the same size
while(aIt.hasNext())
{
    Object A o = new ObjectA(aIt.next(), bIt.next(), cIt.next());
    thisList.add(o);
}
John3136
  • 28,809
  • 4
  • 51
  • 69
0
// do all the assertions to check that the sizes are the same
if (a.size() != b.size() || a.size() != c.size()) {
  throw new IllegalStatException("Wrong size");
}

List<ObjectA> objects = new ArrayList<ObjectA>();
for (int i = 0; i < a.size(); i++) {
  String aStr = a.get(i);
  String bStr = b.get(i);
  String cStr = c.get(i);

  ObjectA obj = new ObjectA(aStr, bStr, cStr);
  objects.add(obj);
}

Comment below makes a good point - this could potentially be an O(n^2) solution in the case of a linked list (or any other list which has O(n) access time to access arbitrary elements)

I82Much
  • 26,901
  • 13
  • 88
  • 119
0
for(int i = 0; i < 3; i++) {
    ObjectA oa = new ObjectA();

    oa.testA = a[i];
    oa.testB = b[i];
    oa.testC = c[i];

    thislist.Add(oa);
}
Anthony
  • 12,177
  • 9
  • 69
  • 105
WangHu
  • 11
  • 1
  • 4
0

Well, assuming you mapped object is of type

public class Triplet{
    private final String jedi;
    private final String sith;
    private final String padawan;

    public Triplet(String jedi, String sith, String padawan){
        this.jedi = jedi;
        this.sith = sith;
        this.padawan = padawan;
    }
}

We could define a mapping iterator:

public static class TripletIterator implements Iterator<Triplet>{

    Iterator<String> jedis;
    Iterator<String> siths;
    Iterator<String> padawans;

    public TripletIterator(List<String> jedis, List<String> sith, List<String> padawns){
        this.jedis = jedis.iterator();
        this.siths = sith.iterator();
        this.padawans = padawns.iterator();
    }

    @Override
    public boolean hasNext() {
        return jedis.hasNext() && siths.hasNext() && padawans.hasNext();
    }

    @Override
    public Triplet next() {
        return new Triplet(jedis.next(), siths.next(), padawans.next());
    }

    @Override
    public void remove() {
       throw new UnsupportedOperationException();
    }
}

Then you could simply:

List<String> jedis = asList("Obiwan","Yoda","Luke");
List<String> siths = asList("Lord Sidious", "Darth Mul", "Darth Vader");
List<String> padawans = asList("Anakin", "Jarjar", "Poncho");

Iterator<Triplet> iter = new TripletIterator(jedis, siths, padawans);
while(iter.hasNext()){
    Triplet t = iter.next();
    System.out.println(t);
}
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205