Java/使用while循环替换简单数组中的元素
我有两个数组(不是ArrayList): 我需要创建一个while循环,将值从数组B插入数组a。 例如,如果位置/索引K=5,则结果应为:Java/使用while循环替换简单数组中的元素,java,arrays,Java,Arrays,我有两个数组(不是ArrayList): 我需要创建一个while循环,将值从数组B插入数组a。 例如,如果位置/索引K=5,则结果应为: A: [0, 5, 10, 15, 20, 25, 50, 100, 150, 200, 250, 30, 35, 40, 45]; 这样B[]的内容就可以在索引K处插入到A[]中。这将向右推送现有内容,并删除推送到数组边界之外的任何内容 然后我需要使用do while循环打印出新的a值 我需要使用两个循环来完成这一切:1while循环用于替换,1do w
A: [0, 5, 10, 15, 20, 25, 50, 100, 150, 200, 250, 30, 35, 40, 45];
这样B[]的内容就可以在索引K
处插入到A[]中。这将向右推送现有内容,并删除推送到数组边界之外的任何内容
然后我需要使用do while
循环打印出新的a值
我需要使用两个循环来完成这一切:1while循环用于替换,1do while循环用于打印。即使这不是必须的,这些都是给我的条件
在我的问题背景下,作业中提到的是:
根据索引为K的元素将数组B的所有元素嵌套在数组A中。
void mergep(ArrayList<Integer> a[], ArrayList<Integer> b[], ArrayList<Integer> c)
{
int acursor = c;
int bcursor = 0;
while(acursor<= b.length())
{
int shiftcursor = b.length()-1;
while(shiftcursor>acursor)
{
a[shiftcursor+1] = a[shiftcursor];
shiftcursor--;//This shifts all the existing entries to the right
}
a[acursor] = b[bcursor];
bcursor++;
acursor++;
}
抱歉,如果我不清楚,我是5天前开始学习编程的,我对应该提到的细节不太熟悉,也许到目前为止我编写的代码有助于提供更多的上下文(第一个while循环是我尝试执行此线程中要求的操作的地方):
导入java.util.array;
导入java.util.Random;
导入java.util.Scanner;
公共级雨衣{
公共静态void main(字符串[]args){
int A[]=新int[15];
int B[]=新int[5];
for(int i=10;i 如果(K>=0&&KI根据输出修改代码
// while (count > K && count <= A.length) {
// int x = 0;
// x++;
// A[K] = B[x];
// count++;
//
// }
我也是堆栈溢出中的noobie。
首先,我建议您使用ArrayList,因为它们在学习后更灵活。
它不是一个干净或优化的代码,但是,
尝试将以下算法添加到代码中。
我是一个试图活跃、学习和参与堆栈溢出的noobie。这段代码可能有错误错误。但我希望我能帮助解释该算法的方法。
void mergep(ArrayList<Integer> a[], ArrayList<Integer> b[], ArrayList<Integer> c)
{
int acursor = c;
int bcursor = 0;
while(acursor<= b.length())
{
int shiftcursor = b.length()-1;
while(shiftcursor>acursor)
{
a[shiftcursor+1] = a[shiftcursor];
shiftcursor--;//This shifts all the existing entries to the right
}
a[acursor] = b[bcursor];
bcursor++;
acursor++;
}
void合并(ArrayList a[],ArrayList b[],ArrayList c)
{
int acursor=c;
int bcursor=0;
while(acursoracursor)
{
a[shiftcursor+1]=a[shiftcursor];
shiftcursor--;//将所有现有条目向右移动
}
a[acursor]=b[bcursor];
bcursor++;
acursor++;
}
}您的代码
int count = K;
while (count > K && count <= A.length) {
int x = 0;
x++;
A[K] = B[x];
count++;
}
int count=K;
而(计数>K&&count K
)
条件count必须先移动A
中的元素,然后才能插入B
中的元素。首先从数组后面移动所有值,以避免丢失任何元素。将元素向右移动一步时,元素的移动应如下所示:
1. [5, 10, 15]
2. [5, 10, 15] 15
3. [5, 10, 10] 15
4. [5, 5, 10] 15
5. [2, 5, 10] 15
元素15将从数组中删除,但5和10仍将保留在数组中。然后可以为5的旧位置分配一个新值,例如2
从数组的最后一个索引开始迭代并移动元素,以便B
的元素可以容纳。但是,与上面的步骤不同,将元素移动到B
的长度。当达到索引K
时停止迭代,因为不再插入或移动更多元素。下面的java代码移动数组A的元素
并指定B
中的值。第一个if语句避免索引越界。第二个if语句检查B
中的元素是否应插入A
int idxA = A.length - 1;
int idxB = B.length - 1;
while(idxA >= K){
if (idxA + B.length < A.length)
A[idxA + B.length] = A[idxA];
if (idxA < B.length + K){
A[idxA] = B[idxB];
idxB--;
}
idxA--;
}
intidxa=A.length-1;
int idxB=B.长度-1;
while(idxA>=K){
if(idxA+B.长度
如果您不确定代码是如何工作的,请在代码的每一步都用纸和笔写下每个数组的当前状态。您需要很多。但首先:到目前为止您尝试了什么?您的问题还不清楚。我不知道如何将B的值添加到a中产生该结果。我也不知道K是什么,以及它扮演的角色s、 你应该先向你自己(和我们)解释清楚你应该做什么。下次请在问题中发布你的代码,而不是在外部网站上。也许你想要a[k]=5*K;
这与询问者的指示相冲突。很遗憾,我无法在此分配中使用ArrayList。但无论如何,谢谢!是否特别提到合并数组或仅按要求打印输出。合并然后打印。
1. [5, 10, 15]
2. [5, 10, 15] 15
3. [5, 10, 10] 15
4. [5, 5, 10] 15
5. [2, 5, 10] 15
int idxA = A.length - 1;
int idxB = B.length - 1;
while(idxA >= K){
if (idxA + B.length < A.length)
A[idxA + B.length] = A[idxA];
if (idxA < B.length + K){
A[idxA] = B[idxB];
idxB--;
}
idxA--;
}