2

I am making a program that keeps track of races. I would like to be able to move one race a certain number of spots away from where it used to be and then have all the things in between move down.

I have an array of "Rounds", and each "Round" has an array of "Races".

Round[] rounds = {new Round(), new Round(), new Round()};

Each Round has an array of Races.

Race[] races = {new Race(), new Race(), new Race()};

I could also represent like this:

0.0, 0.1, 0.2; 1.0, 1.1, 1.2; 2.0, 2.1, 2.2

I want to take the 0.2 object and move it forward 3 spots in between 1.2 and 2.0. Keep in mind that doing this will move the object in between arrays and therefore, have to move everything in between the three arrays. So it will look like this after moving:

0.0, 0.1, 1.0; 1.1, 1.2, 0.2; 2.0, 2.1, 2.2

Again, this is moving the object between arrays and not in the same one.

8
  • you need a linked list in given required operations i suppose, instead of array or 2D array. if you can specify how many races contained in a round, and you need to move races around all the time, try my approach. Commented Sep 18, 2014 at 16:49
  • I imagine you probably want to maintain distinct references to individual rounds. In that case, you don't want a single linked list of all the races across all rounds. However, changing the arrays to lists for both rounds and races will help you in your bookkeeping as list.add(0) will automatically handle pushing existing entry indices up. Removing works the same way, such that you could fairly easily move a race between rounds simply by removing from the one list, then adding to the other. Commented Sep 18, 2014 at 16:57
  • @HuStmpHrrr I don't do this all the time, just in the event that a racer isn't ready and I can move it to a later time. Commented Sep 18, 2014 at 16:58
  • @JDS Do you mean, convert this array of arrays 0.0, 0.1, 0.2; 1.0, 1.1, 1.2; 2.0, 2.1, 2.2 to one list? ``0.0, 0.1, 0.2, 1.0, 1.1, 1.2, 2.0, 2.1, 2.2` Then move the race around. Then convert back into the array of arrays? Commented Sep 18, 2014 at 17:01
  • well, if you want to simulate real races, i can assume it's a workflow and in this case, linked list just work perfectly and solve your problem in O(1). Commented Sep 18, 2014 at 17:08

2 Answers 2

1

Here is something you can do. It is a solution to your direct question, look at the comments for other solution that are probably simpler and more efficient.

You can organize an array of arrays (a matrix pretty much) such that every index of the outer array corresponds to one of your arrays:

index
  0    [0.0, 0.1, 0.2]
  1    [1.0, 1.1, 1.2]
  2    [2.0, 2.1, 2.2]

Now we have to shift data around. This can be done something like this:

  1. Save the element you are moving into a temporary variable
  2. Shift all the data in the array you are moving the element from
  3. Shift all data in arrays between the array you are moving from and the array you are moving to
  4. Shift all data in the array you are moving the element to
  5. Store the saved element from the temporary variable to appropriate position

Code:

void move(double[][] arrays, int indexFrom, int posFrom, int indexTo, int posTo) {

    // step 1
    double movedElement = arrays[indexFrom][posFrom];

    // step 2
    // shift all elements that are to the right of the moved element by 1 position left
    for(int j = posFrom + 1; j < arrays[indexFrom].length; j++) {
        arrays[indexFrom][j - 1] = arrays[indexFrom][j];
    }

    // step 3
    // shift all arrays between the array you are moving from 
    // and the array you are moving to
    for(int i = indexFrom + 1; i < indexTo; i++) {
        // move the first element of the next array
        // as the last element of the previous array
        int indexOfLast = arrays[i-1].length - 1;
        arrays[i - 1][indexOfLast] = arrays[i][0];
        // shift remaining elements of the next array
        for(int j = 1; j < arrays[i].length; j++) {
            arrays[i][j - 1] = arrays[i][j];
        }
    }

    // step4

    // store the first element of the array we are moving to
    // as the last element of the previous array
    int indexOfLast = arrays[indexTo - 1].length - 1;
    arrays[indexTo - 1][indexOfLast] = arrays[indexTo][0];

    // starting from the position we are moving to, shift all elements 
    // to the left      
    for(int j = 1; j <= posTo; j++) {
        arrays[indexTo][j - 1] = arrays[indexTo][j];
    }

    // step 5
    // store the moved element at its proper position
    arrays[indexTo][posTo] = movedElement;
}

Calling the function to move element from position 2 within array 0 to position 2 within array 1:

move(data, 0, 2, 1, 2);

On the input:

| 0.0 0.1 0.2 |
| 1.0 1.1 1.2 |
| 2.0 2.1 2.2 |

Produces output:

| 0.0 0.1 1.0 |
| 1.1 1.2 0.2 |
| 2.0 2.1 2.2 |

Click for the full running test code

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

1 Comment

I think this is what I am looking for, let me try it out when I get home.
0

One option might to just have 2 arrays in the top level class: the first is an array of Races that contains all of the races:

Race[] races = {new Race(), ...};

The other is an array of the indicies of each round's starting race. In your example it would be:

int[] roundStartIndicies = {0, 3, 6};

Then moving the races becomes a lot easier. When you want to then get the rounds, you can do so easily as well. For example if you want the second race in the second round you can do

races[roundStartIndicies[1] + 1]

Sometimes looking at a 2D problem in a 1D way helps make code cleaner.

Edit: It depends how you are accessing the data, but you might want to make the Races a LinkedList as HuStmpHrrr suggested. However you lose constant time random access of races.

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.