Skip to main content
added 1 character in body
Source Link
user17233545
user17233545

Try this.

public enum Direction { UP, RIGHT, DOWN, LEFT; publicprivate static final Direction[] VALUES = values(); public Direction next() { return VALUES[(ordinal() + 1) % VALUES.length]; } } public static void main(String[] args) { Direction a = Direction.LEFT; Direction b = Direction.UP; System.out.println(a.next() == b); } 

output:

true 

Try this.

public enum Direction { UP, RIGHT, DOWN, LEFT; public static final Direction[] VALUES = values(); public Direction next() { return VALUES[(ordinal() + 1) % VALUES.length]; } } public static void main(String[] args) { Direction a = Direction.LEFT; Direction b = Direction.UP; System.out.println(a.next() == b); } 

output:

true 

Try this.

public enum Direction { UP, RIGHT, DOWN, LEFT; private static final Direction[] VALUES = values(); public Direction next() { return VALUES[(ordinal() + 1) % VALUES.length]; } } public static void main(String[] args) { Direction a = Direction.LEFT; Direction b = Direction.UP; System.out.println(a.next() == b); } 

output:

true 
deleted 8 characters in body
Source Link
user17233545
user17233545

Try this.

public enum Direction { UP, RIGHT, DOWN, LEFT; public static final intDirection[] sizeVALUES = values().length;; public boolean isNextOf(Direction dnext() { return VALUES[(d.ordinal() + 1) % size == ordinal();VALUES.length]; } } public static void main(String[] args) { Direction a = Direction.LEFT; Direction b = Direction.UP; System.out.println(ba.isNextOfnext(a) == b); } 

output:

true 

Try this.

public enum Direction { UP, RIGHT, DOWN, LEFT; public static final int size = values().length; public boolean isNextOf(Direction d) { return (d.ordinal() + 1) % size == ordinal(); } } public static void main(String[] args) { Direction a = Direction.LEFT; Direction b = Direction.UP; System.out.println(b.isNextOf(a)); } 

output:

true 

Try this.

public enum Direction { UP, RIGHT, DOWN, LEFT; public static final Direction[] VALUES = values(); public Direction next() { return VALUES[(ordinal() + 1) % VALUES.length]; } } public static void main(String[] args) { Direction a = Direction.LEFT; Direction b = Direction.UP; System.out.println(a.next() == b); } 

output:

true 
Source Link
user17233545
user17233545

Try this.

public enum Direction { UP, RIGHT, DOWN, LEFT; public static final int size = values().length; public boolean isNextOf(Direction d) { return (d.ordinal() + 1) % size == ordinal(); } } public static void main(String[] args) { Direction a = Direction.LEFT; Direction b = Direction.UP; System.out.println(b.isNextOf(a)); } 

output:

true