Tabnine Logo
Arrays
Code IndexAdd Tabnine to your IDE (free)

How to use
Arrays
in
java.util

Best Java code snippets using java.util.Arrays (Showing top 20 results out of 286,155)

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);
  }
});
java.utilArrays

Javadoc

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.

Most used methods

  • asList
    Returns a List of the objects in the specified array. The size of the List cannot be modified, i.e.
  • toString
    Returns a string representation of the contents of the specified array. The string representation co
  • equals
    Returns true if the two specified arrays of booleans areequal to one another. Two arrays are conside
  • sort
    Sorts the specified range of the array into ascending order. The range to be sorted extends from the
  • copyOf
    Copies the specified array, truncating or padding with false (if necessary) so the copy has the spec
  • fill
    Assigns the specified boolean value to each element of the specified array of booleans.
  • stream
  • hashCode
    Returns a hash code based on the contents of the specified array. For any two boolean arrays a and
  • copyOfRange
    Copies the specified range of the specified array into a new array. The initial index of the range (
  • binarySearch
    Searches the specified array of shorts for the specified value using the binary search algorithm. Th
  • deepEquals
    Returns true if the two specified arrays are deeply equal to one another. Unlike the #equals(Object[
  • deepToString
  • deepEquals,
  • deepToString,
  • deepHashCode,
  • setAll,
  • parallelSort,
  • parallelSetAll,
  • spliterator,
  • checkBinarySearchBounds,
  • checkOffsetAndCount,
  • checkStartAndEnd

Popular in Java

  • Creating JSON documents from java classes using gson
  • runOnUiThread (Activity)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • startActivity (Activity)
  • Rectangle (java.awt)
    A Rectangle specifies an area in a coordinate space that is enclosed by the Rectangle object's top-
  • BufferedInputStream (java.io)
    A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the i
  • FileNotFoundException (java.io)
    Thrown when a file specified by a program cannot be found.
  • LinkedList (java.util)
    Doubly-linked list implementation of the List and Dequeinterfaces. Implements all optional list oper
  • JButton (javax.swing)
  • Options (org.apache.commons.cli)
    Main entry-point into the library. Options represents a collection of Option objects, which describ
  • CodeWhisperer alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now