简体   繁体   English

同时迭代两个具有不同大小的列表

[英]Iterating two lists same time which have different size

I have two list X,Y. 我有两个清单X,Y。

Which are lists of Strings . 这是String的列表。

there is a possibility to have the two lists different sizes . 有可能使两个列表的大小不同。

If both lists are of same size of 2 then i can procedd like 如果两个列表的大小都是2,那么我可以像

   for (int i =0;  i<anyList.size(); i++){
     system.out.printLn(X(i) +" "+Y(i));
  }

example result  :

stringX1 stringY1 
stringX2 stringY2

how can i handle the loop which have different sizes 我该如何处理大小不同的循环

example result should be look like this 示例结果应如下所示

example result : 结果示例:

    stringX1 stringY1 
    stringX2 stringY2
    stringX3
    stringX4
Iterator<String> x_it = x.iterator();
Iterator<String> y_it = y.iterator();
while(x_it.hasNext() && y_it.hasNext()){
   System.out.println(x.next() + " " + y.next())
}
while(x_it.hasNext()){
   System.out.println(x.next());
}

while(y_it.hasNext()){
   System.out.println(y.next());
}
for (int i =0;  i<max(X.size(),Y.size()); i++){
  if(i<X.size() && i<Y.size()) {
    print(X.get(i) + " " + Y.get(i));
  } else if(i<Y.size()) {
    print(Y.get(i));
  } else {
    print(X.get(i));
  }
}

Programming a max(int, int) and print(String) method shouldn't be to hard. 编程max(int,int)和print(String)方法不难。

if(arr1.size()>=arr2.size())
    max  = arr1.size();
else
    max = arr2.size();

for(int i=0;i<max;i++)
{
    if(arr1.size() >= i+1)
        System.out.println(arr1.get(i));
    if(arr2.size() >= i+1)
    System.out.println(arr2.get(i));
}
public static void main(String[] args) {
    List<String> l1 = new ArrayList<String>();
    l1.add("Pif");
    l1.add("Paf");
    l1.add("Pouf");
    List<String> l2 = new ArrayList<String>();
    l2.add("Argh!");
    l2.add("Aie!");

    Iterator<String> it1 = l1.iterator();
    Iterator<String> it2 = l2.iterator();

    String s1, s2;
    while (it1.hasNext() || it2.hasNext()) {
        if (it1.hasNext()) {
            s1 = it1.next();
            System.out.print(s1 + " - ");
        }
        if (it2.hasNext()) {
            s2 = it2.next();
            System.out.print(s2);
        }
        System.out.println();
    }


}

Which yields: 产生:

Pif - Argh!
Paf - Aie!
Pouf - 
List<String> shorter = Arrays.asList("red", "blue");
List<String> longer = Arrays.asList("one", "two", "three", "four");

int size = Math.max(shorter.size(), longer.size());

StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {

  if (shorter.size() > i) {
    sb.append(shorter.get(i)).append('\t');
  } else {
    sb.append("\t\t");
  }

  if (longer.size() > i) {
    sb.append(longer.get(i));
  }

  sb.append('\n');
}

System.out.println(sb.toString());

output 输出

red   one
blue  two
      three
      four
 int size = x.size() > y.size ? x.size() : y.size();
 for (int i =0;  i< size ; i++)
 {
     System.out.printLn( ( x.size() > i + 1 ? X(i) : "") +" "+( y.size() > i + 1 ? Y(i) : "") );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同一对象的两个列表具有不同的哈希码 - Two lists of same objects have different hash codes 当两个列表的大小不同时,将两个列表中具有相同索引的元素的值相加 - Add the values of elements with the same index from two lists when the size of both the lists are different 如何同时工作具有相同优先级的两个不同线程? - How to work concurrently two different threads which have the same priority? Java将两个不同大小的列表合并到HashMap - Java Merge two Lists of different size to a HashMap 将两个不同大小的列表合并到地图 - combine two different size lists to a Map 使用Java流将两个大小相同(类型不同)的列表合并到域对象列表中 - Combine two lists of same size (and different type) into list of domain objects using java streams 我如何创建两个具有相同Columnmodel但具有不同模型的Jtables - How can i create two Jtables which have same Columnmodel, but they have different models 在Java中,如何同时遍历两个列表? - In Java, how to traverse two lists at the same time? 遍历列表并允许其同时删除项目(NOT ITERATOR) - Iterating through a list and allowing it to have an item removed at the same time (NOT ITERATOR) 比较类的两个列表而不迭代列表 - Comparing two Lists of a class without iterating the lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM