两个ArrayList 怎么进行循环比较,在线等。。。。
有ArrayList A 跟 BAid name1 甲1 乙1 丙2 丁2 戊3 哥Bid name1 A1 B2 C2 D3 E3 F怎样形成一个新的ArrayList cCAname Bname甲 A乙 B丙 NULL丁 C戊 D哥 ENULL F就是list A 与 list B id相等的拼起来,如果不相等,用Null代替,在线等。。。谢谢 大虾
import java.util.ArrayList;import java.util.List;class Node{ private int id ; private String name ; public Node(){ } public Node(int id, String name){ this.id = id ; this.name = name ; } public int getId(){ return this.id ; } public void setId(int id){ this.id = id ; } public String getName(){ return this.name ; } public void setName(String name){ this.name = name ; }}class Node1{ private String name1 ; private String name2 ; public Node1(){ } public Node1(String name1, String name2){ this.name1 = name1 ; this.name2 = name2 ; } public String getName1(){ return this.name1 ; } public void setName1(String name1){ this.name1 = name1 ; } public String getName2(){ return this.name2 ; } public void setName2(String name2){ this.name2 = name2 ; } @Override public String toString() { return "Node1 [name1=" + name1 + ", name2=" + name2 + "]"; } }public class Test3{ public static void main(String [] args){ List<Node> listA = new ArrayList<Node>() ; listA.add(new Node(1, "甲")) ; listA.add(new Node(1, "乙")) ; listA.add(new Node(1, "丙")) ; listA.add(new Node(2, "丁")) ; listA.add(new Node(2, "戊")) ; listA.add(new Node(3, "哥")) ; List<Node> listB = new ArrayList<Node>() ; listB.add(new Node(1, "A")) ; listB.add(new Node(1, "B")) ; listB.add(new Node(2, "C")) ; listB.add(new Node(2, "D")) ; listB.add(new Node(3, "E")) ; listB.add(new Node(3, "F")) ; int idxA = 0 , idxB = 0 ; int oldId = -1 ; int aId = 0, bId = 0; Node nA = null ,nB = null ; List<Node1> listC = new ArrayList<Node1> () ; while(idxA < listA.size() && idxB < listB.size()){ nA = (Node)listA.get(idxA) ; aId = nA.getId() ; nB = (Node)listB.get(idxB) ; bId = nB.getId() ; if (oldId == -1){ oldId = nA.getId() ; }// System.out.println(" idxA: "+ idxA + " -- "+ aId + " -- " + oldId + " --- " + bId // +" -- "+ " idxB: "+ idxB); if (aId == oldId){ if (bId == oldId){ listC.add(new Node1(nA.getName(), nB.getName()) ) ; idxA++ ; idxB++ ; }else{ listC.add(new Node1(nA.getName(), null) ) ; idxA++ ; } }else if (bId == oldId){ listC.add(new Node1(null, nB.getName()) ) ; idxB++ ; }else{ oldId = aId ; if (bId == oldId){ listC.add(new Node1(nA.getName(), nB.getName()) ) ; idxA++ ; idxB++ ; }else{ listC.add(new Node1(nA.getName(), null) ) ; idxA++ ; } } } while(idxA < listA.size()){ nA = listA.get(idxA) ; idxA++ ; listC.add(new Node1(nA.getName(), null) ) ; } while(idxB < listB.size()){ nB = listB.get(idxB) ; idxB++ ; listC.add(new Node1(null, nB.getName()) ) ; } for (int i =0 ; i < listC.size(); i++){ System.out.println(listC.get(i).toString()) ; } }}
[解决办法]
public class ArrayTest { public static void main(String args[]) { List<String[]> a = new ArrayList<String[]>(); List<String[]> b = new ArrayList<String[]>(); String[] a1 = { "1", "一" }; String[] a2 = { "1", "二" }; String[] a3 = { "1", "三" }; String[] a4 = { "2", "四" }; String[] a5 = { "2", "五" }; String[] a6 = { "3", "六" }; a.add(a1);a.add(a2);a.add(a3);a.add(a4);a.add(a5);a.add(a6); String[] b1 = { "1", "A" }; String[] b2 = { "1", "B" }; String[] b3 = { "2", "C" }; String[] b4 = { "2", "D" }; String[] b5 = { "3", "E" }; String[] b6 = { "3", "F" }; String[] b7 = { "4", "G" }; b.add(b1);b.add(b2);b.add(b3);b.add(b4);b.add(b5);b.add(b6);b.add(b7); List<String[]> result = new ArrayList<String[]>(); int indexA = 0; int indexB = 0; for (int i = 0; i < Math.max(a.size(),b.size()); i++) { String[] temp1 = new String[2]; String[] temp2 = new String[2]; if(indexA < a.size()){ temp1 = a.get(indexA); } else{ temp1[0] = ""+Integer.MAX_VALUE; temp1[1] = null; } if(indexB < b.size()){ temp2 = b.get(indexB); } else{ temp2[0] =""+Integer.MAX_VALUE; temp2[1] = "null"; } String[] tempResult = new String[2]; if (temp1[0].equals(temp2[0])){ tempResult[0] = temp1[1]; tempResult[1] = temp2[1]; result.add(tempResult); indexA++; indexB++; } else if (Integer.parseInt(temp1[0]) > Integer.parseInt(temp2[0])) { tempResult[0] = "null"; tempResult[1] = temp2[1]; result.add(tempResult); indexB++; } else if (Integer.parseInt(temp1[0]) < Integer.parseInt(temp2[0])) { tempResult[0] = temp1[1]; tempResult[1] = "null"; result.add(tempResult); indexA++; } } for(String[] ss:result){ System.out.println(ss[0]+","+ss[1]); } }}