I much prefer using Enums to store system wide string constants, makes it easy to create additional functionality and I think it is generally accepted best practice.
public enum Cat {
A() {
@Override public void execute() {
System.out.println("A clicked");
}
},
B() {
@Override public void execute() {
System.out.println("B Clicked");
}
};
//template method
public abstract void execute();
}
then you can call it like so :
String clickedItem = "A";
Cat.valueOf(clickedItem).execute(); //will print A is clicked
or you could also the Command pattern without enums....
public interface Cat {
void do();
}
public class CatA() implements Cat {
void do() {//put your code here}
}
public class CatB() implements Cat {
void do() {//put your other code here}
}
then build a Map object and populate it with Cat instances:
catMap.put("A", new CatA());
catMap.put("B", new CatB());
then you can replace your if/else if chain with:
catMap.get(clickedItem).do();
There are a few ways to do this.
In addition to the six 3x3 arrays containing the colors of the stickers, you could have a 3x3x3 array representing the solid cubies. (The interior cell of this array can be ignored.) The cells of the 3x3x3 array could then contain integers (or pointers to more detailed objects) describing the original location of each cubie. You just have to make sure you do the appropriate "rotation" of the 3x3x3 array each time you "rotate" the stickers.
Or you could just put everything in one 5x5x5 array. The central 3x3x3 array could represent the solid parts of the cube and the central 3x3 array of cells on each face of the 5x5x5 array could represent the stickers.
Another alternative: just a 3x3x3 array, in which each cell contains a pointer to a structure. The structure identifies the original location of the cubie and the colors and locations of the stickers on its faces. The structure also contains a modifiable integer that describes the orientation of the cubie: a particular value (say, 0) for the orientation the cubie "should" have in a fully-solved cube, and 23 other values representing the other possible orientations of the cubie after a sequence of rotations. You also need a function (which could be a simple table lookup) that says which orientation results from each possible quarter-turn applied to each possible orientation. You will also probably find use for a function that says, for each orientation, what face of the original (orientation 0) cubie is on the top, bottom, left, and so forth. To rotate a slice of the cube, you have to permute the contents of the moving cells, of course, but you also have to correctly modify the orientation of every cubie in the rotated slice (including the cubie in the center of the face for a face rotation, even though the location of the cubie does not move).
Best Answer
The Java Collections framework is something that you should browse. There are also some types that aren't array-like (Set and Map) but you will find that you will use them often. There are many different types of collections, and some have advantages over others for certain types of operations.
Classic arrays
First, the thing that isn't in the collections framework. The Array. Not ArrayList, but rather the classic
int[]
. The advantage of the array is that it closely maps to what memory is.foo[42]
is to lookup. Many system level calls that give you a fixed number of items are arrays and they are good to know."1,2,3".split(",")
returns back an array for example.The problem with arrays is that they aren't that flexible. Adding an element to the end of an array that already is at is maximum capacity means allocating a new, larger array and then doing an
System.arraycopy()
(javadoc) to copy all the elements from one array to another array in a timely manner.To remove an element from the list you need to remove it and then walk the remainder of the list to have everything be contagious again. If you want to remove
foo[4]
from the list, you then assignfoo[4] = foo[5]
andfoo[5] = foo[6]
and so on down to the end of the list. Thats if you have to do it by hand. You can also use thatSystem.arraycopy
mentioned to do it for you given the right set of arguments. Something like:I might have that wrong, but thats the general idea. Copying the array back on itself. It should work.
Lists
As you see, arrays are a bit awkward to work with. You've got do it all by hand. Its good to know, but a pain to practice. There are two lists that are in common use that make this easier for you. For the most part, Lists look like arrays.
You call methods on them, but you are fetching the 4th element via method call instead of via direct access through the array. See that
foo.remove(5)
. Thats it. You're done. You have removed an element from the list and deleted it. Much easier to remember.ArrayList
The ArrayList is backed by an Array. This means that doing
foo.get(4)
runs fast, butfoo.add(42, foo.size()+1)
is likely to be slow because it has to do all of that moving of things around to allocate a new list (yes, I know that the list can be longer than the size, but lets assume that its the same size as its actual list).Instead of having to remember how to do all the shuffling, you've got nice methods to do your work for you. It doesn't mean that its not done (the work is still done behind the scenes of the interface), but its something that you don't have to worry about.
So, the ArrayList is for:
LinkedList
For some reason, everyone's goto list implementation is the ArrayList. I don't know why. Many times when I'm working with Lists, the LinkedList is the one that is more applicable. You keep adding things to the end. Rarely do you want the nth element. You just want to iterate over them.
This is what the LinkedList is good for. It does fast remove and append and its iterator is just as fast as the ArrayList. Its just if you want
foo.get(400)
its going to be a bit slower than the ArrayList implement.The LinkedList is also a Deque too, but people forget about that. That lets you access the LinkedList as a double ended queue (nice for implementing a Stack or Queue).
So, if you are just appending and iterating, the LinkedList is for you.
Note that the O(index) there is a bit of an award bit. It will index depending on which way is faster to get there (from the end or start). So if the LinkedList is 100 long, and you get(10), it starts from the start. If you ask for get(90), it starts from the end. So get(index) is never more than get(length/2), but thats all just factors that don't matter when looking at Big O.