Java 将两个列表合并为一个列表的程序
我必须合并这两个列表。这是我目前尚未完成的编码程序。我无法取得任何进一步的进展。我需要帮忙完成这项任务。合并列表应包含所有提供的编号。另外,如果你愿意,你能留下一个详细的解决过程给我学习吗Java 将两个列表合并为一个列表的程序,java,arrays,merge,jgrasp,Java,Arrays,Merge,Jgrasp,我必须合并这两个列表。这是我目前尚未完成的编码程序。我无法取得任何进一步的进展。我需要帮忙完成这项任务。合并列表应包含所有提供的编号。另外,如果你愿意,你能留下一个详细的解决过程给我学习吗 public class Lab18bvst { public static void main(String[] args) { int[] jsaList1 = {101, 105, 115, 125, 145, 165, 175, 185, 195, 225, 235,
public class Lab18bvst
{
public static void main(String[] args)
{
int[] jsaList1 = {101, 105, 115, 125, 145, 165, 175, 185, 195, 225, 235, 275, 305, 315, 325, 335, 345, 355, 375, 385};
int[] jsaList2 = {110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 270, 280, 320, 350, 400};
Array list1 = new Array(jsaList1,"List #1");
Array list2 = new Array(jsaList2,"List #2");
Array list3 = new Array("Merged List");
list3.merge(list1,list2);
list1.display();
list2.display();
list3.display();
}
}
class Array
{
private ArrayList<Integer> list;
private int size;
private String listName;
public Array(String ln)
{
list = new ArrayList<Integer>();
size = 0;
listName = ln;
}
public Array(int[] jsArray, String ln)
{
list = new ArrayList<Integer>();
size = jsArray.length;
listName = ln;
for (int j = 0; j < size; j++)
list.add( new Integer( jsArray[j] ));
}
public void display()
{
System.out.println("\n" + listName + ":\n");
System.out.println(list + "\n");
}
public void merge(Array that, Array theOther)
{
int thatIndex, theOtherIndex;
thatIndex = theOtherIndex = 0;
for (int j = 1; j <= that.size + theOther.size; j++)
{
if (thatIndex >= that.size)
{
while(theOtherIndex < theOther.size)
{
this.list.add(theOther.list.get(theOtherIndex));
theOtherIndex++;
}
}
else if (theOtherIndex >= theOther.size)
{
while(thatIndex < that.size)
{
this.list.add(theOther.list.get(thatIndex));
thatIndex++;
}
}
else if
else
}
}
}
连接两个列表的最简单方法
List<String> newList = new ArrayList<String>();
newList.addAll(listOne);
newList.addAll(listTwo);
我需要100万美元尽快!!!1!@约翰内斯库恩:我需要一匹小马尽快!!一请阅读-总结是,这不是一个理想的方式来解决志愿者,可能会适得其反获得答案。请不要将此添加到您的问题中。@halfer PONIES!!!比一百万美元更重要。我喜欢。这是一种连接,而不是一种合并。你不能消除重复项。@guillaumegirod vitouchkina将newList设置为一种集合类型,它会的。