数组变量输入帮助[关闭]
首先,我必须创建一个长度为 n 的数组,输入变量来填充该数组,然后在数组位置 k 处,我必须将所有数组位置 k 及向上推 1,并将 x 的值放入数组位置 k 中。但如果 k = n,则将 x 放入 n+1 中。
首先,我在使 k 和 x 的整数工作时遇到问题。由于某种原因,代码将 n 设置为第一个输入,并在设置 n 后立即将 k 和 x 设置为与 n 相同。
其次,我在将数组扩展到 n+1 时遇到问题。我知道不只是给我答案,但我需要一些前进的方向。
import java.util.Scanner;
public class hw2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int x = scan.nextInt();
int[] a = new int[n];
for(int i = 0; i<n; i++)
{
a[i] = scan.nextInt();
}
n++;
final int LENGTH = a.length - 1;
for(int j=LENGTH; j>k; j--)
{
a[j] = a[j-1];
}
a[k] = x;
for(int h = 0; h < n; h++)
{
System.out.println("location " + h + " is " + a[h]);
}
}
}
nkxa[0]... a[n-1] 的样本输入
5 3 7 2 3 5 11 13
分别为。
使用该输入运行代码后,n 应该 = 6 并且数组应该
a[0] = 2
a[1] = 3
a[2] = 5
a[3] = 7
a[4] = 11
a[5] = 13
编辑:我读到的问题完全错误。底部的提示说“假设数组的大小至少为 n+1” ...
First of all, I have to create an array of length n, input variables to fill that array, then at array location k, I have to push all arrays location k and up by 1 and put the value of x into array location k. But if k = n, then put x in n+1.
First of all, I am having a problem making the ints for k and x work. For some reason the code sets n to the first input and sets k and x to the same as n immediately when n is set.
Secondly, I am having trouble extending the array to n+1. I know don't just give me the answer but I need some direction on where to go.
import java.util.Scanner;
public class hw2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int x = scan.nextInt();
int[] a = new int[n];
for(int i = 0; i<n; i++)
{
a[i] = scan.nextInt();
}
n++;
final int LENGTH = a.length - 1;
for(int j=LENGTH; j>k; j--)
{
a[j] = a[j-1];
}
a[k] = x;
for(int h = 0; h < n; h++)
{
System.out.println("location " + h + " is " + a[h]);
}
}
}
sample input for n k x a[0]... a[n-1] is
5 3 7 2 3 5 11 13
respectively.
After the code runs with that input, n should = 6 and the array should be
a[0] = 2
a[1] = 3
a[2] = 5
a[3] = 7
a[4] = 11
a[5] = 13
EDIT: I read the problem completely wrong. A hint at the bottom said "Assume that the array is of at least size n+1"
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来一切都很好,包括这个:
之后就变得有点粗略了。这个问题可以有几种解释......
对我来说,这意味着“覆盖”位置 k 中的值,除非 k == n。至于这几种方式——这取决于你的作业的措辞方式。如果数组的起始大小并不重要,那么您可以从数组大小 n+1 (或 n+someArbitraryValue)而不是 n 开始。如果您必须根据 k==n 检查将大小保持在 n 或 n+1,那么当 k==n 时,您需要设置一个大小为 n+1 的新数组并将 a 中的值复制到其中。
希望这会有所帮助,但又不会放弃太多。
It looks like everything including this is fine:
After that is gets a little sketchy. The problem could be interpreted a couple ways...
To me, this means "overwrite" the value in location k unless k == n. As to the couple ways - it depends on how your assignment is worded. If it doesn't matter what size the array is to start with then you could start with your array size being n+1 (or n+someArbitraryValue) instead of n. If you have to keep the size at n or n+1 depending on the k==n check then when k==n you need to setup a new array with size n+1 and copy the values from a into it.
Hope this helps without giving too much away.
很难知道你在问什么,因为你的代码与你写的不一致。据我了解,n 元素数组已经填满。在这种情况下,您不能只在末尾附加一个元素,您需要创建一个新数组并将所有数据从旧数组复制到新数组。
它的工作原理如下:
然后像平常一样将 k 放入 new[n+1] 中。请注意,如果数组不为空,则只需在 n 处插入 x 即可执行上述操作。
It's hard to know just what you're asking because your code is inconsistent with what you wrote. From what I understand the n element array is already filled to capacity. In this case you can't just append an element at the end, you need to make a new array and copy all of the data from the old array to the new array.
It would work something like this:
Then just put k in at new[n+1] like you would normally. Note that if the array were not empty you would only need to do the above if you were inserting x at n.
你可以只使用ArrayList
You could just use ArrayList