0

I'm trying to put the lowest five numbers from one array (they are array objects) into an array by themselves. Here's my code, this is after pulling the array objects into their own array and sorting that array in ascending order. From there I'm trying to keep the lowest 5 items in the array. If there's 5 or more scores I figured slicing the array to keep the first 5 would be the easiest method, if there's less than 5, just copy from one array to the other.

  if(scoreID > 5){
     int lowestScores = scoreArray.slice(0,6);
  } 
  else { 
     for(int i=0;i<scoreID;i++) {
        int[] lowestScores = new int[scoreID];
        lowestScores[i] = scoreArray[i];}
  }

scoreID is just a place holder for the number of scores that the primary array is stored.

The error I'm getting is...

Golfer.java:194: error: cannot find symbol
    int lowestScores = scoreArray.slice(0,6);
                                  ^
symbol:   method slice(int,int)
location: variable scoreArray of type int[]
1 error
5
  • 1
    Change it to int[] lowestScores = scoreArray.slice(0,6); Commented Feb 17, 2016 at 1:09
  • You are trying to assign an int[] to an int variable. Commented Feb 17, 2016 at 1:12
  • And there is no slice() method in Java unless you implemented it yourself. Commented Feb 17, 2016 at 1:15
  • @pp_ even if he implemented slice() I don't think he can use a . after his scoreArray to use the method. Maybe could be something like slice(scoreArray, 0, 6) Commented Feb 17, 2016 at 1:53
  • @Shadowfax You're right, to be able to do scoreArray.slice() he would have to actually modify the Arrays class in the java.util package. Commented Feb 17, 2016 at 4:05

2 Answers 2

2

Try using Arrays.copyOf

In you code

int[] lowestScores  = Arrays.copyOf(scoreArray, 5);

As per the javadocs

a copy of the original array, truncated or padded with zeros to obtain the specified length

By the way slice is a javaScript method.

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

1 Comment

Thank you, will mark answer. Didn't realize slice was JS not java.
1
    int [] b = new int [] {0, 1, 2, 3, 4, 5};


int [] copiedto = Arrays.copyOfRange(b, 0, 4);

Give it a try hope this may help rather than slice method.

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.