-1

I am running this code:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;


class Ideone
{
 public static int solution(int X, int[] A) {
    int[] myNumbers = new int[X];
    for (int i = 0; i < A.length; i++){
        myNumbers[A[i]] = A[i];

       }
        return -1;

}

public static void main (String[] args) throws java.lang.Exception
{
    // your code goes here
    int[] A = {1,3,1,4,2,3,5,4};
    System.out.println(solution(5,A));
}
}

However, I get a run-time error. I don't know why. I need to store the value of the array in A in another array with that value as an index. I.e myNumbers[4] = 4.

1
  • However, I get a run-time error. I'm not even going to include the error in my question. I couldn't be bothered. I expect you to figure it out for yourself... Commented May 1, 2016 at 23:31

1 Answer 1

1

myNumber indexes goes from 0 to 4, at some point you are trying to access the index 5 which does not exist

So either pass 6 to solution or use myNumber[A[i]-1] (so myNumber[0] = 1)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.