Jason Brown
Jason Brown

Reputation: 59

Java Array Slice error

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

Upvotes: 0

Views: 1145

Answers (2)

SmashCode
SmashCode

Reputation: 761

    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.

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44854

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.

Upvotes: 2

Related Questions