5 Replies - 407 Views - Last Post: 21 January 2013 - 05:28 PM Rate Topic: -----

#1 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 02-June 12

Shifting Elements in a Array

Posted 20 January 2013 - 01:58 PM

I have a high score list for a text based java game and I want my high score list to show the scores for the ten recent players. So technically itís not a high score list, itís just a list that shows the last ten player names who played the game and their scores.
My list works for the first ten players but after that it goes out of bounds and I donít know how to shift the elements in the list up by one. So if ten players have played and the score list is viewed, you will see their names and points, but after the 11th player plays and registers their score I want the list to remove the first player, and shift the others up by one then once the tenth element is blank it will register the 11th player's name and score.

I have included the snippet code for the scores as well as the whole code for my java project - (I tried uploading my project folder in a .zip file but the attachments keeps telling me "Error The server returned an error during upload") - so that anyone can understand my objective through the game and play my text based java game to understand what am I trying to do in case my objectives in this post are not clear.

Code for the method only is pasted below.
Full program code in notepad txt file - Program Code in text file
full program including the project folder in a zip file - Full program in project folder

Thank you and any help is appreciated.

Codes for the recent scores
public static void scoreChange(int prevScore[], int points) {
        for (int i3 = i2 + 1; i2 < i3; i3--){
            prevScore[i2] = points;
        }
    }

public static void nameChange(String prevScoreName[], String newName) {
        for (int i3 = i2 + 1; i2 < i3; i3--){
            prevScoreName[i2] = newName;
        }
    }

public static void printScores(int highScore[], String highScoreName[]) //a method that prints high scores
    {
        System.out.println("High Scores");
        //FOR Loop to print High Scores
        for (int i3 = 0; i3 < TopScores.length; i3++){
            System.out.println("Name: " + highScoreName[i3] + "     " + "Points: " + highScore[i3]);
        }
        System.out.println("");
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Shifting Elements in a Array

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Shifting Elements in a Array

Posted 20 January 2013 - 02:01 PM

I suggest you use what's known as a circular buffer that contains the 10 scores with a pointer pointing to last score saved. If the pointer exceeds the size of the buffer, pointer = 10, then the pointer is reset to 0. You can use the modulus operator to keep the pointer in bounds.
Was This Post Helpful? 1
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • Posts: 2,859
  • Joined: 30-January 11

Re: Shifting Elements in a Array

Posted 20 January 2013 - 02:04 PM

I think there is an underlying issue here. For example printScores method takes an array of scores and an array of names as parameters. It would be a lot easier to create a Player class which has properties like score and name and just print objects of that class out instead.

This will probably sort out your other issues as well. Instead of changing names around, you simply move the position of the Player object, better yet have the Player class store its position.

You also have some interesting looping variables going on. i1, i2, i3. Not sure whats going on with that as i2 is never defined in this snippet.

Mac also has a good tutorial Moving Away from Parallel Arrays

This post has been edited by Ryano121: 20 January 2013 - 02:04 PM

Was This Post Helpful? 1
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1047
  • View blog
  • Posts: 4,452
  • Joined: 09-June 09

Re: Shifting Elements in a Array

Posted 20 January 2013 - 07:36 PM

Shifting array is a lot of work for such a simple operation. Maybe you should try looking into a different data structure, maybe a queue would be better.

As long as there is less than 10 elements in the queue, simply enqueue data to the end. If there is 10 or more elements in the queue, then dequeue the first element then add the new element to the end of the queue.

A circular buffer could also be used, as mentioned above.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Shifting Elements in a Array

Posted 20 January 2013 - 08:30 PM

You seem to miss the whole concept about for() loop

public static void scoreChange(int prevScore[], int points) {
        for (int i3 = i2 + 1; i2 < i3; i3--){
            prevScore[i2] = points;


as the value of i2 is not changed within the loop.... no need a loop at all just
            prevScore[i2] = points;


will do the job

Same thing here
        for (int i3 = i2 + 1; i2 < i3; i3--){
            prevScoreName[i2] = newName;


Was This Post Helpful? 1
  • +
  • -

#6 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 02-June 12

Re: Shifting Elements in a Array

Posted 21 January 2013 - 05:28 PM

View Postjjl, on 21 January 2013 - 02:36 AM, said:

Shifting array is a lot of work for such a simple operation. Maybe you should try looking into a different data structure, maybe a queue would be better.

As long as there is less than 10 elements in the queue, simply enqueue data to the end. If there is 10 or more elements in the queue, then dequeue the first element then add the new element to the end of the queue.

A circular buffer could also be used, as mentioned above.


jjl Thanks man but would you please be kind enough to explain queues more because I just started learning Java this semester. Any example code would also be very helpful and appreciated.

View Postpbl, on 21 January 2013 - 03:30 AM, said:

You seem to miss the whole concept about for() loop

public static void scoreChange(int prevScore[], int points) {
        for (int i3 = i2 + 1; i2 < i3; i3--){
            prevScore[i2] = points;


as the value of i2 is not changed within the loop.... no need a loop at all just
            prevScore[i2] = points;


will do the job

Same thing here
        for (int i3 = i2 + 1; i2 < i3; i3--){
            prevScoreName[i2] = newName;



pblThanks for the for loop correction man, i just understood that i dont really need a for loop because i have the variable i2 which goes up by one each time the user plays the game.

And to the other generous contributors in this topic, thank you for taking the time to help a fellow Java newbie. I appreciate the help you have given me, but since I'm a beginner i didn't really understand your posts. I just started learning Java in Computer Science Grade 11, so i haven't learned to use Classes or Circular Buffers yet. But i guess that was my fault because i should have mentioned that I'm a beginner so contributors would be more in-depth and less complicated through their posts. Sorry about that and thanks anyways.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1