我想加入两个阵列但不像通常那样
{1,2,3} {4,5,6}
{1,2,3,4,5,6}
我想像他们一样加入他们
{1,2,3} {4,5,6}
{1,4,2,5,3,6}
有什么建议吗?
我不想连接两个数组,而是拉链它们
答案 0 :(得分:3)
如果两者的数组大小不相等,本程序也适用。很乐意帮助
public class Assignment{
public static void main(String[] args){
int [] arr1 = new int[]{1,2,3};
int [] arr2 = new int[]{4,5,6,7,8};
int [] arr3 = new int[arr1.length + arr2.length];
int biglength = 0;
if(arr1.length > arr2.length){
biglength = arr1.length;
}else{
biglength = arr2.length;
}
for(int i=0,j=0; i< biglength; i++){
if(i<arr1.length && i<arr2.length){
arr3[j++] = arr1[i];
arr3[j++] = arr2[i];
}else if(i<arr1.length){
arr3[j++] = arr1[i];
}else{
arr3[j++] = arr2[i];
}
}
for(int j= 0 ; j<arr3.length; j++){
System.out.print(arr3[j]);
}
}
}
答案 1 :(得分:2)
这是一种使用Java 8流的技术:
PRIVATE int func_name()
{
return 0;
}
答案 2 :(得分:0)
VerA - 在List和Integer上,返回Integer []; VerB - 在数组和int上,返回int []; 混合n个数组,其长度可以不同。
public static Integer[] arraysMixVerA(Integer[]... arrays) {
List<Integer> list = new ArrayList<>();
int max=-1;
for (Integer[] array : arrays) {
max = Math.max(max, array.length);
}
for (int i = 0; i < max*arrays.length; i++) {
list.add(null);
}
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
list.set(j * arrays.length + i, arrays[i][j]);
}
}
for(int i=0;i<list.size();i++){
if(list.get(i)==null)
list.remove(i);
}
return list.toArray(new Integer[list.size()]);
}
public static int[] arraysMixVerB(int[]... arrays) {
int max=-1;
int sumaIndex=0;
for (int[] array : arrays) {
max = Math.max(max, array.length);
sumaIndex=sumaIndex+array.length;
}
Integer[] temp=new Integer[max*arrays.length]; //For an array of
//type int, the default value is 0. For an array of type Integer,
//the default value is null. Thus could not be distinguish zeros with arrays
//that are arguments methods of zeros from the array "temp".
int[] target=new int[sumaIndex];
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
temp[j * arrays.length + i]=arrays[i][j];
}
}
for(int i=0,j=0;i<temp.length;i++){
if(temp[i]!=null){
target[j++]=temp[i];
}
}
return target;
}
答案 3 :(得分:0)
O(m)+ O(n)是您在此处可以达到的最大值,此处已经提供了准确的答案。 但是,如果阵列大小更高,那么就说10 ^ 7如果你想减少计算时间,只需要强加到你的机器内核上,并且在代码中有轻微的复杂性,你可以在这里使用并发。
int[] arr1 = new int[10000010];
int[] arr2 = new int[10000000];
int end, endFinal;
int[] output = new int[arr1.length+arr2.length];
if( arr1.length < arr2.length )
end = arr1.length;
else
end = arr2.length;
endFinal = arr1.length + arr2.length;
T obj1 = new T( arr1, output, 0, end );
T obj2 = new T( arr2, output, 1, end );
Thread t1 = new Thread(obj1);
Thread t2 = new Thread(obj2);
t1.start();
t2.start();
t1.join();
t2.join();
for( int j = 2*end; j<endFinal; j++)
{
if(endFinal == arr1.length)
output[j] = arr1[end++];
else
output[j] = arr1[end++];
}
}
class T implements Runnable {
int[] arr, output;
int start, end;
public T ( int[] arr, int[] output, int start, int end)
{
this.arr = arr;
this.output = output;
this.start = start;
this.end = end;
}
public void run (){
int count = 0;
for (int i = start; i< end; i+=2)
output= arr[count++];
}
}
因此,假设m小于n,则两个线程将填充m个元素,并且将正常填充其余n - m个形式。 这似乎是一个复杂的解决方案,只应在必要时使用。人们可能看不到较小阵列的差异。