public class ListMerge
{
public static void main( String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println ("Input length of arraylist 1:");
int n = input.nextInt();
ArrayList x = new ArrayList();
ArrayList y = new ArrayList();
for (int i = 0; i < n; i++)
{
System.out.println ("Input x[ " + i +"] :" );
x.add(new Integer(i));
}
System.out.println ("Input length of arraylist 2:");
int m = input.nextInt();
for (int i = 0; i < m; i++)
{
System.out.println ("Input y[ " + i +"] :" );
y.add(new Integer(i));
}
List<Integer> all = new ArrayList<Integer>();
all.addAll(x);
all.addAll(y);
System.out.println(all);
}
}
Run Code Online (Sandbox Code Playgroud)
我这样做了,但它没有从用户那里获取价值。请告诉我为什么....
直接来自维基百科:
function merge(left,right)
var list result
while length(left) > 0 and length(right) > 0
if first(left) ? first(right)
append first(left) to result
left = rest(left)
else
append first(right) to result
right = rest(right)
end while
if length(left) > 0
append left to result
else
append right to result
return result
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5767 次 |
| 最近记录: |