Reputation: 450
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.
Upvotes: 2
Views: 421
Reputation: 2573
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.
Upvotes: 0
Reputation: 35481
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:
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
Upvotes: 1