Java Collections.rotate Examples

Java Collections.rotate - 21 examples found. These are the top rated real world Java examples of java.util.Collections.rotate extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  public static void main(String[] args) {
    List<String> list = new ArrayList<String>();

    Collections.addAll(list, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J");
    System.out.println(list);
    System.out.println("list.subList(3,8): " + list.subList(3, 8));
    System.out.println("list.subList(3,8).get(2): " + list.subList(3, 8).get(2));

    System.out.println("list.subList(3,8).set(2,\"B\"):");
    list.subList(3, 8).set(2, "B");
    System.out.println(list);
    System.out.println("list.indexOf(\"B\"): " + list.indexOf("B"));
    System.out.println("list.subList(3,8).indexOf(\"B\"): " + list.subList(3, 8).indexOf("B"));
    System.out.println(list);
    System.out.println("Collections.reverse(list.subList(3,8)):");
    Collections.reverse(list.subList(3, 8));
    System.out.println(list);
    System.out.println("Collections.rotate(list.subList(3,8), 2):");
    Collections.rotate(list.subList(3, 8), 2);
    System.out.println(list);
    System.out.println("Collections.fill(list.subList(3,8), \"X\"):");
    Collections.fill(list.subList(3, 8), "X");
    System.out.println(list);
    list.subList(3, 8).clear();
    System.out.println(list);
  }
Example #2
0
  public static void main(String args[]) {
    String os[] = {"GNU/Linux", "Windows 7", "Solaris", "Amiga OS", "FreeBSD", "Mac OS X"};

    LinkedList<String> al_operating_systems =
        new LinkedList<>(Arrays.asList(os)); // Lista di os
    Collections.sort(al_operating_systems); // ordina la collezione
    System.out.print("Collezione ordinata: ");
    System.out.println(al_operating_systems);

    int ix =
        Collections.binarySearch(
            al_operating_systems, "Be OS", null); // ricerca se presente l'elemento Be Os
    if (ix < 0) // se non è presente aggiungilo
    al_operating_systems.add(-(ix + 1), "Be OS");

    System.out.print("Collezione con elemento Be OS aggiunto: ");
    System.out.println(al_operating_systems);

    Collections.rotate(al_operating_systems, 3); // ruota gli elementi di 3 posizioni
    System.out.print("Collezione con elementi ruotati: ");
    System.out.println(al_operating_systems);

    System.out.print("Elemento della collezione minore: ["); // elemento più piccolo
    System.out.println(Collections.min(al_operating_systems) + "]");

    System.out.print("Elemento della collezione maggiore: ["); // elemento più grande
    System.out.println(Collections.max(al_operating_systems) + "]");
  }
 private Component getNextComponent() {
   Component component;
   synchronized (components) {
     component = components.get(0);
     Collections.rotate(components, 1);
   }
   return component;
 }
  /**
   * method to add a recent file with its associated attributes
   *
   * @param file the recent file object
   * @param attributes the string attributes array
   * @throws FileNotFoundException
   * @throws IOException
   */
  public void add(File file, String[] attributes) throws FileNotFoundException, IOException {
    // process only if the file is a valid file
    if (file != null) {
      // create a recent file object based on the input parameter
      RecentFile recentFile = new RecentFile(file, attributes);

      // now if the recent file list doesnt contian the recent file then
      if (!recentFilesList.contains(recentFile)) {

        // check if the recent file list size is less than the max record size
        if (recentFilesList.size() < maxRecords) {

          // if yes then add the recent file to the recent files list
          recentFilesList.add(recentFile);

          // rotate the list one time to make the last added (that is the current object added)
          // element to be the first element in the list
          Collections.rotate(recentFilesList, 1);
        } else {

          // else if the max record size have been reached then
          // rotate to make the last element in the list to be at the first position
          Collections.rotate(recentFilesList, 1);

          // replace the first position with the new recent file object
          recentFilesList.set(0, recentFile);
        }
      } // else if the the recent file list does contain the recent file then
      else {

        // removes the recent file with the same file object
        recentFilesList.remove(recentFile);

        // adds the recent file with new attributes
        recentFilesList.add(recentFile);
      }

      // save the recent files list to the config file
      save();

      // load the recent files menu items
      loadRecentFilesMenuItems();
    }
  }
Example #5
0
  private void actualizarJugadorQueDebeJugar() {

    try {
      Jugador jugador = this.manoActual.obtenerGanador();
      int posicion = this.jugadoresEnJuego.indexOf(jugador);
      Collections.rotate(jugadoresEnJuego, -posicion);
      this.jugadorQueDebeJugar = this.jugadoresEnJuego.getFirst();
      this.jugadorQueDebeCantar = this.jugadorQueDebeJugar;
    } catch (NoHayGanadorHuboEmpateException e) {
      this.actualizarTurnos();
    }
  }
 static {
   List<Boolean> addFlags = new ArrayList<Boolean>();
   for (int i = 0; i < Fragment.values().length; i++) {
     addFlags.add(RandomUtil.nextBoolean());
   }
   List<List<Boolean>> testFlags = new ArrayList<List<Boolean>>();
   for (int i = 0; i < Fragment.values().length; i++) {
     Collections.rotate(addFlags, 1);
     testFlags.add(new ArrayList<Boolean>(addFlags));
   }
   FLAGS = TestUtils.createData(testFlags.toArray());
   List<Range<?>> ranges = new ArrayList<Range<?>>();
   for (Fragment fragment : Fragment.values()) {
     switch (fragment) {
       case DOES_INVERT:
         ranges.add(Range.getInstance(false, true));
         ranges.add(Range.getInstance(false, true));
         break;
       case DELAY:
       case OSCILLATION_OFFSET:
       case OSCILLATION_PERIOD:
         ranges.add(Range.getInstance(RandomUtil.nextInteger()));
         ranges.add(Range.getInstance(RandomUtil.nextInteger()));
         break;
       default:
         ranges.add(Range.getInstance(RandomUtil.nextFloat()));
         ranges.add(Range.getInstance(RandomUtil.nextFloat()));
         break;
     }
   }
   RANGES = ranges.toArray(new Range<?>[0]);
   TEMPLATE =
       NodeConfiguration //
           .builder() //
           .setDelay(Range.getInstance(0), null) //
           .setInvertFlag(Range.<Boolean>openRange(), null) //
           .setEnergy(Range.<Float>openRange(), null) //
           .setEnergyDecay(Range.<Float>openRange(), null) //
           .setExcitatoryScale(Range.<Float>openRange(), null) //
           .setInhibitoryScale(Range.<Float>openRange(), null) //
           .setOscillationMaximum(Range.<Float>openRange(), null) //
           .setOscillationMinimum(Range.<Float>openRange(), null) //
           .setOscillationOffset(Range.<Integer>openRange(), null) //
           .setOscillationPeriod(Range.<Integer>openRange(), null) //
           .setScale(Range.<Float>openRange(), null) //
           .setShift(Range.<Float>openRange(), null) //
           .setTwitchMaximum(Range.<Float>openRange(), null) //
           .setTwitchMinimum(Range.<Float>openRange(), null) //
           .setTwitchProbability(Range.<Float>openRange(), null) //
           .newInstance();
 }