开发者

Array variable input help [close]

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 开发者_如何学Cn+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" ...


It looks like everything including this is fine:

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();
    }

After that is gets a little sketchy. The problem could be interpreted a couple ways...

then at array location k, input the variable x. But if k = n, then put x in n+1

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.


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:

int[] oldArr = new int[n]; //old array, full
int[] newArr = new int[2*n];//new array
for(i=0;i<n;i++){
  new[i] = old[i];
}

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.


You could just use ArrayList

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜