private void reset() {
   isLooseMatch = false;
   Collections.fill(localVarMatches, null);
   for (int i = 0; i < paramNodeMatches.size(); i++) {
     this.paramNodeMatches.set(i, null);
   }
 }
 public static void main(String[] args) {
   try {
     eq(
         0,
         (new BallsSeparating_2())
             .minOperations(new int[] {1, 1, 1}, new int[] {1, 1, 1}, new int[] {1, 1, 1}),
         6);
     eq(
         1,
         (new BallsSeparating_2()).minOperations(new int[] {5}, new int[] {6}, new int[] {8}),
         -1);
     eq(
         2,
         (new BallsSeparating_2())
             .minOperations(
                 new int[] {4, 6, 5, 7}, new int[] {7, 4, 6, 3}, new int[] {6, 5, 3, 8}),
         37);
     eq(
         3,
         (new BallsSeparating_2())
             .minOperations(
                 new int[] {7, 12, 9, 9, 7},
                 new int[] {7, 10, 8, 8, 9},
                 new int[] {8, 9, 5, 6, 13}),
         77);
     eq(
         4,
         (new BallsSeparating_2())
             .minOperations(
                 new int[] {
                   842398, 491273, 958925, 849859, 771363, 67803, 184892, 391907, 256150, 75799
                 },
                 new int[] {
                   268944, 342402, 894352, 228640, 903885, 908656, 414271, 292588, 852057, 889141
                 },
                 new int[] {
                   662939, 340220, 600081, 390298, 376707, 372199, 435097, 40266, 145590, 505103
                 }),
         7230607);
     int[] a = new int[50];
     int[] b = new int[50];
     int[] c = new int[50];
     fill(a, 1);
     fill(b, 1);
     fill(c, 1);
     eq(5, (new BallsSeparating_2()).minOperations(a, b, c), 7230607);
   } catch (Exception exx) {
     System.err.println(exx);
     exx.printStackTrace(System.err);
   }
 }
Example #3
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 #4
0
  int Query() {

    // You have to report the shortest path from Steven and Grace's home (vertex 0)
    // to reach their chosen hospital (vertex 1)
    //
    // write your answer here

    q = new PriorityQueue<IntegerPair>();
    weights = new Vector<Integer>();
    weights.setSize(V);
    Collections.fill(weights, Integer.MAX_VALUE); // initially all infinite

    q.offer(new IntegerPair(0, 0));
    weights.set(0, 0);

    return dijkstra();
  }
 public static void main(String[] args) {
   print(list);
   print(
       "'list' disjoint (Four)?: "
           + Collections.disjoint(list, Collections.singletonList("Four")));
   print("max: " + Collections.max(list));
   print("min: " + Collections.min(list));
   print("max w/ comparator: " + Collections.max(list, String.CASE_INSENSITIVE_ORDER));
   print("min w/ comparator: " + Collections.min(list, String.CASE_INSENSITIVE_ORDER));
   List<String> sublist = Arrays.asList("Four five six".split(" "));
   print("indexOfSubList: " + Collections.indexOfSubList(list, sublist));
   print("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist));
   Collections.replaceAll(list, "one", "Yo");
   print("replaceAll: " + list);
   Collections.reverse(list);
   print("reverse: " + list);
   Collections.rotate(list, 3);
   print("rotate: " + list);
   List<String> source = Arrays.asList("in the matrix".split(" "));
   Collections.copy(list, source);
   print("copy: " + list);
   Collections.swap(list, 0, list.size() - 1);
   print("swap: " + list);
   Collections.shuffle(list, new Random(47));
   print("shuffled: " + list);
   Collections.fill(list, "pop");
   print("fill: " + list);
   print("frequency of 'pop': " + Collections.frequency(list, "pop"));
   List<String> dups = Collections.nCopies(3, "snap");
   print("dups: " + dups);
   print("'list' disjoint 'dups'?: " + Collections.disjoint(list, dups));
   // Getting an old-style Enumeration:
   Enumeration<String> e = Collections.enumeration(dups);
   Vector<String> v = new Vector<String>();
   while (e.hasMoreElements()) v.addElement(e.nextElement());
   // Converting an old-style Vector
   // to a List via an Enumeration:
   ArrayList<String> arrayList = Collections.list(v.elements());
   print("arrayList: " + arrayList);
 }
Example #6
0
 /**
  * Returns a Tree containing {@code n} values supplied by a given Supplier {@code s}.
  *
  * @param <T> Component type of the Tree
  * @param n The number of elements in the Tree
  * @param s The Supplier computing element values
  * @return A Tree of size {@code n}, where each element contains the result supplied by {@code s}.
  * @throws NullPointerException if {@code s} is null
  */
 static <T> Tree<T> fill(int n, Supplier<? extends T> s) {
   Objects.requireNonNull(s, "s is null");
   return Collections.fill(n, s, Tree.empty(), Tree::of);
 }
Example #7
0
 /**
  * Returns an Array containing {@code n} values supplied by a given Supplier {@code s}.
  *
  * @param <T> Component type of the Array
  * @param n The number of elements in the Array
  * @param s The Supplier computing element values
  * @return An Array of size {@code n}, where each element contains the result supplied by {@code
  *     s}.
  * @throws NullPointerException if {@code s} is null
  */
 public static <T> Array<T> fill(int n, Supplier<? extends T> s) {
   Objects.requireNonNull(s, "s is null");
   return Collections.fill(n, s, empty(), Array::of);
 }
  public void testLinkedListOperations() throws Exception {

    List<Integer> numbers = new LinkedList<>(Arrays.asList(0, 1, 2, 3, 4));

    assertEquals(5, numbers.size());

    for (int i = 0; i < numbers.size(); i++) {
      assertEquals(i, (int) numbers.get(i));
    }

    numbers.add(2, 10);
    assertEquals(10, (int) numbers.get(2));

    Collections.sort(numbers);
    assertEquals(10, (int) numbers.get(numbers.size() - 1));

    int index = Collections.binarySearch(numbers, 10);
    assertEquals(5, index);

    System.out.println("Before shuffle:");

    for (Integer i : numbers) {
      System.out.print(i + " ");
    }

    Collections.shuffle(numbers);

    System.out.println("\nAfter shuffle:");

    for (int i : numbers) {
      System.out.print(i + " ");
    }

    Collections.sort(numbers);
    Collections.reverse(numbers);

    assertEquals(10, (int) numbers.get(0));
    assertEquals(0, (int) numbers.get(numbers.size() - 1));

    int max = Collections.max(numbers);
    assertEquals(10, max);

    int min = Collections.min(numbers);
    assertEquals(0, min);

    List<Integer> copy = Arrays.asList(0, 0, 0, 0, 0, 0);
    Collections.copy(copy, numbers);

    for (int i = 0; i < copy.size(); i++) {
      assertEquals((int) copy.get(i), (int) numbers.get(i));
    }

    Collections.fill(numbers, 100);

    for (int i : numbers) {
      assertEquals(100, i);
    }

    numbers = new LinkedList<>(Arrays.asList(0, 1, 2, 3, 4, 1));
    int frequency = Collections.frequency(numbers, 1);
    assertEquals(2, frequency);

    copy = Arrays.asList(0, 0, 0, 0, 0, 0);
    boolean disjoint = Collections.disjoint(numbers, copy);
    assertFalse(disjoint);

    copy = Arrays.asList(100, 101, 102);
    disjoint = Collections.disjoint(numbers, copy);
    assertTrue(disjoint);
  }
  @Test
  public void test() {
    List<String> worker = new ArrayList<String>();
    worker.add("Sunny");
    worker.add("Edition");
    worker.add("Clinton");

    // 排序
    Collections.sort(worker);
    System.out.println(worker);

    // 二分法查找,必须先排序
    int index = Collections.binarySearch(worker, "Sunny");
    System.out.println("binary search index=" + index);

    // 复制,目标列表的个数必须大于等于源列表的个数,此方法比较鸡肋
    List<String> list = new ArrayList<String>();
    list.add("a");
    list.add("b");
    list.add("c");
    Collections.copy(list, worker);
    System.out.println("list is " + list);

    // 替换
    Collections.fill(list, "A");
    System.out.println("list after fill A is " + list);

    // 返回指定 collection 中等于指定对象的元素数
    int times = Collections.frequency(list, "A");
    System.out.println("A frequency is  " + times);

    // 获取最大最小数据
    System.out.println("min data of worker is " + Collections.min(worker));
    System.out.println("max data of worker is " + Collections.max(worker));

    // 复制多次
    list = Collections.nCopies(10, "default");
    System.out.println("nCopies list is " + list);

    // 反转
    Collections.reverse(worker);
    System.out.println("reverse worker is " + worker);

    // 打乱顺序
    Collections.shuffle(worker);
    System.out.println("shuffle worker is " + worker);

    // 包含指定对象的Set
    Set<String> set = new HashSet<String>(Collections.singleton("sunny"));
    System.out.println("singleton set " + set);

    // 包含指定对象的List
    list = new ArrayList<String>(Collections.singletonList("sunny"));
    System.out.println("singletonList is " + list);

    // 包含指定键值对的Map
    Map<String, String> map = new HashMap<String, String>(Collections.singletonMap("key", "value"));
    System.out.println("singletonMap is " + map);

    // 交换指定位置的元素
    Collections.swap(worker, 0, 1);
    System.out.println("after swap worker is " + worker);
  }
Example #10
0
 public static BinaryIndex begin(int digits) {
   BinaryIndex tmp = new BinaryIndex(digits);
   Collections.fill(tmp.b_, false);
   return tmp;
 }
Example #11
0
  public static void main(String[] args) {

    List<MyClass> list = new ArrayList<MyClass>();

    list.add(new MyClass("Василий"));
    list.add(new MyClass("Павел"));
    list.add(new MyClass("Андрей"));
    list.add(new MyClass("Андрей"));
    list.add(new MyClass("Петр"));
    list.add(new MyClass("Анжелика"));

    System.out.println("Оригинал");
    for (MyClass mc : list) {
      System.out.println("Item:" + mc);
    }
    System.out.println();

    // Смешивание
    Collections.shuffle(list);
    System.out.println("Смешивание");
    for (MyClass mc : list) {
      System.out.println("Item:" + mc);
    }
    System.out.println();

    // Обратный порядок
    Collections.reverse(list);
    System.out.println("Обратный порядок");
    for (MyClass mc : list) {
      System.out.println("Item:" + mc);
    }
    System.out.println();

    // "Проворачивание" на определенное количество
    Collections.rotate(list, 2); // Число может быть отрицательным - тогда порядок будет обратный
    System.out.println("Проворачивание");
    for (MyClass mc : list) {
      System.out.println("Item:" + mc);
    }
    System.out.println();

    // Обмен элементов
    Collections.swap(list, 0, list.size() - 1);
    System.out.println("Обмен элементов");
    for (MyClass mc : list) {
      System.out.println("Item:" + mc);
    }
    System.out.println();

    // Замена
    Collections.replaceAll(list, new MyClass("Андрей"), new MyClass("Алексей"));
    System.out.println("Замена");
    for (MyClass mc : list) {
      System.out.println("Item:" + mc);
    }
    System.out.println();

    // Сортировка
    // Collections.sort(list);   // Этот запрос не пройдет т.к. класс MyClass
    // не реализует интерфейс Comparable
    System.out.println("Сортировка Comparable класса");
    List<MyClassCompare> listSort = new ArrayList<MyClassCompare>();
    System.out.println("Без сортировки");
    for (MyClass mc : list) {
      listSort.add(new MyClassCompare(mc.toString()));
      System.out.println("Item sort:" + mc);
    }
    Collections.sort(listSort);
    System.out.println("После сортировки");
    for (MyClassCompare mc : listSort) {
      System.out.println("Item sort:" + mc);
    }
    System.out.println();

    // Сортировка с классом Comparator
    System.out.println("Сортировка с Comparator");
    System.out.println("Без сортировки");
    for (MyClass mc : list) {
      System.out.println("Item:" + mc);
    }
    Collections.sort(list, new MyClassComparator());
    System.out.println("После сортировки");
    for (MyClass mc : list) {
      System.out.println("Item:" + mc);
    }
    System.out.println();

    // Копирование - здесь обязательно надо иметь нужные размеры
    List<MyClass> list2 = new ArrayList<MyClass>();
    // Поэтому заполняем список. Хоть чем-нибудь.
    for (MyClass mc : list) {
      list2.add(null);
    }
    // Компируем из правого аргумента в левый
    Collections.copy(list2, list);
    System.out.println("Копирование");
    for (MyClass mc : list2) {
      System.out.println("Item:" + mc);
    }
    System.out.println();

    // Полная замена
    Collections.fill(list2, new MyClass("Антон"));
    System.out.println("Полная замена");
    for (MyClass mc : list2) {
      System.out.println("Item:" + mc);
    }
    System.out.println();
  }
 @Override
 protected List<Object> getItems(int start, int end) {
   ArrayList<Object> result = new ArrayList<Object>(end - start);
   Collections.fill(result, new Object());
   return result;
 }