如何交错列表

时间:2012-03-23 03:19:59

标签: java arraylist collections

我脑子里有一些困扰我的东西已经有一段时间了,希望我能得到一些善良的帮助......:D

无论如何,这就是我想做的事情:

我确实有一些列表,比如说

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"};

我有一个对象的数组列表,我想将这些字符串添加到,即

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

在我的ObjectA课程中,我有:

public class ObjectA{

   String testA;
   String testB;
   String testC;
}

我的最终输出应该是:

List<ObjectA> - 
  • testA1
  • testB1
  • testC1

  • testA2

  • testB2
  • testC2

  • testA3

  • testB3
  • testC3

如何从给定的个人列表中设置我的对象中的这些字符串?

抱歉我的英语不好,我希望这是可以理解的。

我考虑过this,但不太明白。

我的代码是

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...
}

4 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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);
}

下面的评论提出了一个很好的观点 - 对于链表(或任何其他具有O(n)访问时间来访问任意元素的列表),这可能是O(n ^ 2)解决方案

答案 2 :(得分: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);
}

答案 3 :(得分:0)

好吧,假设您的映射对象是

类型
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;
    }
}

我们可以定义一个映射迭代器:

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();
    }
}

然后你可以简单地说:

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);
}