java.util.Arrays类的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(196)

本文整理了Java中java.util.Arrays类的一些代码示例,展示了Arrays类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Arrays类的具体详情如下:
包路径:java.util.Arrays
类名称:Arrays

Arrays介绍

[英]This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.

The methods in this class all throw a NullPointerException, if the specified array reference is null, except where noted.

The documentation for the methods contained in this class includes briefs description of the implementations. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort(Object[]) does not have to be a MergeSort, but it does have to be stable.)

This class is a member of the Java Collections Framework.
[中]此类包含用于操作数组(例如排序和搜索)的各种方法。此类还包含一个静态工厂,允许将数组作为列表查看。
如果指定的数组引用为null,则此类中的方法都会抛出NullPointerException,除非另有说明。
此类中包含的方法的文档包括对实现的简要描述。此类描述应视为实施说明,而不是规范的一部分。只要遵守规范本身,实现者应该可以随意替换其他算法。(例如,sort(Object[])使用的算法不必是MergeSort,但必须是稳定的。)
此类是Java Collections Framework的成员。

代码示例

canonical example by Tabnine

private void usingArrayList() {
 ArrayList<String> list = new ArrayList<>(Arrays.asList("cat", "cow", "dog"));
 list.add("fish");
 int size = list.size(); // size = 4
 list.set(size - 1, "horse"); // replacing the last element to "horse"
 String removed = list.remove(1); // removed = "cow"
 String second = list.get(1); // second = "dog"
}

canonical example by Tabnine

public long getDirectorySize(File file) {
 if (!file.exists()) {
  return 0;
 }
 if (file.isFile()) {
  return file.length();
 }
 File[] files;
 if (!file.isDirectory() || (files = file.listFiles()) == null) {
  return 0;
 }
 return Arrays.stream(files).mapToLong(f -> getDirectorySize(f)).sum();
}

代码示例来源:origin: stackoverflow.com

List<String> lines = Arrays.asList("The first line", "The second line");
Path file = Paths.get("the-file-name.txt");
Files.write(file, lines, Charset.forName("UTF-8"));
//Files.write(file, lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND);

代码示例来源:origin: stackoverflow.com

int[] intArray = { 7, 9, 5, 1, 3 };
System.out.println(Arrays.toString(intArray));

代码示例来源:origin: google/guava

/**
 * Returns a copy of {@code multiset} as an {@link ImmutableMultiset} whose iteration order is
 * highest count first, with ties broken by the iteration order of the original multiset.
 *
 * @since 11.0
 */
@Beta
public static <E> ImmutableMultiset<E> copyHighestCountFirst(Multiset<E> multiset) {
 Entry<E>[] entries = (Entry<E>[]) multiset.entrySet().toArray(new Entry[0]);
 Arrays.sort(entries, DecreasingCount.INSTANCE);
 return ImmutableMultiset.copyFromEntries(Arrays.asList(entries));
}

代码示例来源:origin: stackoverflow.com

String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
System.out.println(Arrays.toString(deepArray));
//output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
System.out.println(Arrays.deepToString(deepArray));

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Iterable<Integer> apply(Object v) throws Exception {
    Integer[] array = new Integer[1000 * 1000];
    Arrays.fill(array, 1);
    return Arrays.asList(array);
  }
})

代码示例来源:origin: google/guava

private static void testReverse(short[] input, short[] expectedOutput) {
 input = Arrays.copyOf(input, input.length);
 Shorts.reverse(input);
 assertTrue(Arrays.equals(expectedOutput, input));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Sort the given array with a default AnnotationAwareOrderComparator.
 * <p>Optimized to skip sorting for lists with size 0 or 1,
 * in order to avoid unnecessary array extraction.
 * @param array the array to sort
 * @see java.util.Arrays#sort(Object[], java.util.Comparator)
 */
public static void sort(Object[] array) {
  if (array.length > 1) {
    Arrays.sort(array, INSTANCE);
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testGetValuesUnbounded() {
  ReplaySubject<Object> rs = ReplaySubject.createUnbounded();
  Object[] expected = new Object[10];
  for (int i = 0; i < expected.length; i++) {
    expected[i] = i;
    rs.onNext(i);
    assertArrayEquals(Arrays.copyOf(expected, i + 1), rs.getValues());
  }
  rs.onComplete();
  assertArrayEquals(expected, rs.getValues());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void attributeNames() throws Exception {
  this.attributeAccessor.setAttribute(NAME, VALUE);
  this.attributeAccessor.setAttribute("abc", "123");
  String[] attributeNames = this.attributeAccessor.attributeNames();
  Arrays.sort(attributeNames);
  assertTrue(Arrays.binarySearch(attributeNames, NAME) > -1);
  assertTrue(Arrays.binarySearch(attributeNames, "abc") > -1);
}

代码示例来源:origin: google/guava

private static void testReverse(
  short[] input, int fromIndex, int toIndex, short[] expectedOutput) {
 input = Arrays.copyOf(input, input.length);
 Shorts.reverse(input, fromIndex, toIndex);
 assertTrue(Arrays.equals(expectedOutput, input));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Sort the given factory methods, preferring public methods and "greedy" ones
 * with a maximum of arguments. The result will contain public methods first,
 * with decreasing number of arguments, then non-public methods, again with
 * decreasing number of arguments.
 * @param factoryMethods the factory method array to sort
 */
public static void sortFactoryMethods(Method[] factoryMethods) {
  Arrays.sort(factoryMethods, EXECUTABLE_COMPARATOR);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testGetValues() {
  ReplaySubject<Object> rs = ReplaySubject.create();
  Object[] expected = new Object[10];
  for (int i = 0; i < expected.length; i++) {
    expected[i] = i;
    rs.onNext(i);
    assertArrayEquals(Arrays.copyOf(expected, i + 1), rs.getValues());
  }
  rs.onComplete();
  assertArrayEquals(expected, rs.getValues());
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public List<Integer> apply(Integer a, Integer b, Integer c) {
    return Arrays.asList(a, b, c);
  }
})

代码示例来源:origin: google/guava

private static void testSortDescending(char[] input, char[] expectedOutput) {
 input = Arrays.copyOf(input, input.length);
 Chars.sortDescending(input);
 assertTrue(Arrays.equals(expectedOutput, input));
}

代码示例来源:origin: stackoverflow.com

double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
System.out.println(Arrays.toString(doubleArray));

代码示例来源:origin: spring-projects/spring-framework

/**
 * Sort the given constructors, preferring public constructors and "greedy" ones with
 * a maximum number of arguments. The result will contain public constructors first,
 * with decreasing number of arguments, then non-public constructors, again with
 * decreasing number of arguments.
 * @param constructors the constructor array to sort
 */
public static void sortConstructors(Constructor<?>[] constructors) {
  Arrays.sort(constructors, EXECUTABLE_COMPARATOR);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testGetValues() {
  ReplayProcessor<Object> rs = ReplayProcessor.create();
  Object[] expected = new Object[10];
  for (int i = 0; i < expected.length; i++) {
    expected[i] = i;
    rs.onNext(i);
    assertArrayEquals(Arrays.copyOf(expected, i + 1), rs.getValues());
  }
  rs.onComplete();
  assertArrayEquals(expected, rs.getValues());
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public List<Integer> apply(Integer t1, Integer t2, Integer t3, Integer t4) {
    return Arrays.asList(t1, t2, t3, t4);
  }
});

相关文章