如何从两个单独的数组中移动值 [英] How can I move values around from two separate arrays
本文介绍了如何从两个单独的数组中移动值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里的总菜鸟。我有这个我想用数组做的事情让我说我有两个数组
[0,1,2,3]&& [4,5,6,7]
任何人都可以通过为Java实现提供一些伪代码或实际代码来帮助我,让它看起来像这个
[0,4,1,5,2,6,3,7]
非常感谢你的帮助许多提前谢谢
我尝试过的事情:
到目前为止一些伪代码导致我没有
total noob here. I have this thing I am trying to do with arrays lets say I have two arrays
[0, 1, 2, 3] && [4, 5, 6, 7]
can anyone please help me by either providing some pseudo-code or actual code for a Java implementation to get it to look like this
[0,4, 1, 5, 2, 6, 3,7]
your help is much-appreciated many thank yous in advance
What I have tried:
so far some pseudo code that is leading me no where
推荐答案
hi
i有这个解决方案
hi i have this solution for you
import java.util.Scanner;
public class Test
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int [] ar = new int[4];
int [] ar1 = new int[4];
int [] res = new int[8];
int i;
int k;
for (i=0;i<4;i++)
{
System.out.println("Enter a number");
ar[i]=sc.nextInt();
}
for (i=0;i<4;i++)
{
System.out.println("Enter a number");
ar1[i]=sc.nextInt();
}
for (k=0,i=0;i<4;i++,k=k+2)
{
res[k]=ar[i];
}
for (k=1,i=0;i<4;i++,k=k+2)
{
res[k]=ar1[i];
}
for (i=0;i<8;i++)
{
System.out.print(res[i]+" ");
}
}
}
输出>
output
Enter a number
0
Enter a number
1
Enter a number
2
Enter a number
3
Enter a number
4
Enter a number
5
Enter a number
6
Enter a number
7
0 4 1 5 2 6 3 7
这篇关于如何从两个单独的数组中移动值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文