Advertisement
UniQuet0p1

Untitled

Sep 12th, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class ColorSort {
  4.  
  5. enum Color {red, green, blue}
  6.  
  7. ;
  8.  
  9. public static void main(String[] param) {
  10. Color[] balls = {Color.BLUE, Color.RED, Color.GREEN, Color.BLUE, Color.RED, Color.GREEN, Color.BLUE, Color.RED, Color.GREEN};
  11. sortBalls(balls);
  12. System.out.println(Arrays.toString(balls));
  13. }
  14.  
  15. public static void reorder(Color[] balls) {
  16. int red = 0, green = 0, blue = 0;
  17. for (Color ball : balls) {
  18. switch (ball) {
  19. case RED -> red++;:
  20. case GREEN -> green++;:
  21. case BLUE -> blue++;:
  22. default -> throw new IllegalArgumentException("Incorrect color");
  23. }
  24. }
  25. for (int i = 0; i < red; i++) {
  26. balls[i] = Color.RED;
  27. }
  28. for (int i = red; i < red + green; i++) {
  29. balls[i] = Color.GREEN;
  30. }
  31. for (int i = red + green; i < red + green + blue; i++) {
  32. balls[i] = Color.BLUE;
  33. }
  34. }
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement