Java by API/java.util/Arrays

Материал из Java эксперт
Версия от 14:11, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

Arrays: asList(T[] a)

   
/*
 Output:

a
b
c

 * */
import java.util.*;
public class MainClass {
  public static void main (String args[]) {
    String[] a = new String[]{"a","b","c"};
    List list = Arrays.asList(a);
    Iterator i = list.iterator();
    while (i.hasNext()) {
      System.out.println(i.next());
    }
  }
}





Arrays: binarySearch(int[] a,int key)

   
/**
 *Output: 
Original contents: 0 3 6 9 12 15 18 21 24 27 
Sorted: 0 3 6 9 12 15 18 21 24 27 
After fill(): 0 3 -1 -1 -1 -1 18 21 24 27 
After sorting again: -1 -1 -1 -1 0 3 18 21 24 27 
The value 9 is at location -7
 */
import java.util.Arrays;
public class MainClass {
  public static void main(String args[]) {
    int array[] = new int[10];
    for (int i = 0; i < 10; i++)
      array[i] = 3 * i;
    System.out.print("Original contents: ");
    display(array);
    Arrays.sort(array);
    System.out.print("Sorted: ");
    display(array);
    Arrays.fill(array, 2, 6, -1);
    System.out.print("After fill(): ");
    display(array);
    Arrays.sort(array);
    System.out.print("After sorting again: ");
    display(array);
    System.out.print("The value 9 is at location ");
    int index = Arrays.binarySearch(array, 9);
    System.out.println(index);
  }
  static void display(int array[]) {
    for (int i : array) {
      System.out.print(i + " ");
    }
    System.out.println();
  }
}





Arrays: binarySearch(Object[] a, Object key)

   

import java.util.Arrays;
public class Main {
  public static void main(String[] a) {
    String str[] = { "B", "H", "L", "M", "I", "N", "R" };
    // Ensure list sorted
    Arrays.sort(str);
    for (String s : str) {
      System.out.println(s);
    }
    // Search for element in list
    int index = Arrays.binarySearch(str, "M");
    System.out.println("Found M @ " + index);
  }
}





Arrays: copyOfRange(String[] original, int from, int to)

   
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
public class Main {
  public static void main(String... args) {
    try {
      Class<?> c = Class.forName(args[0]);
      Class[] argTypes = new Class[] { String[].class };
      Method main = c.getDeclaredMethod("main", argTypes);
      String[] mainArgs = Arrays.copyOfRange(args, 1, args.length);
      System.out.format("invoking %s.main()%n", c.getName());
      main.invoke(null, (Object) mainArgs);
      // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
      x.printStackTrace();
    } catch (NoSuchMethodException x) {
      x.printStackTrace();
    } catch (IllegalAccessException x) {
      x.printStackTrace();
    } catch (InvocationTargetException x) {
      x.printStackTrace();
    }
  }
}





Arrays: copyOf(String[] original, int newLength)

 
import java.util.Arrays;
public class Main {
  public static void main(String args[]) {
    System.out.printf("Before (original)\t%s%n", Arrays.toString(args));
    String copy[] = Arrays.copyOf(args, 4);
    System.out.printf("Before (copy)\t\t%s%n", Arrays.toString(copy));
    copy[0] = "A";
    copy[1] = "B";
    copy[2] = "C";
    copy[3] = "D";
    System.out.printf("After (original)\t%s%n", Arrays.toString(args));
    System.out.printf("After (copy)\t\t%s%n", Arrays.toString(copy));
  }
}





Arrays: deepToString(Object[] a)

   
import java.util.Arrays;
public class Main {
  public static void main(String args[]) {
    String s[] = {"a", "b", "c", "d"};
    double d [][]= {
        {0.50, 0.20,  0.20, 0.30},
        {0.50, 1.10,  0.50, 0.80},
        {0.50, 0.70,  0.40},
        {0.50, 0.70},
        {0.50},
    };
    System.out.println(Arrays.toString(s));
    System.out.println(Arrays.deepToString(d));
  }
}





Arrays: equals(boolean[] a, boolean[] a2)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean[] bArr1 = null;
    boolean[] bArr2 = null;
    boolean b = Arrays.equals(bArr1, bArr2); // true
  }
}





Arrays: equals(char[] a, char[] a2)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b = Arrays.equals(new char[] { "a" }, new char[] { "a" }); // true
  }
}





Arrays: equals(double[] a, double[] a2)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b = Arrays.equals(new double[] { 0D }, new double[] { 0D }); // true
  }
}





Arrays: equals(float[] a, float[] a2)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b = Arrays.equals(new float[] { 0F }, new float[] { 0F }); // true
  }
}





Arrays: equals(int[] a, int[] a2)

   
import java.util.Arrays;
public class Main {
  public static void main(String[] a) {
    int[] i = new int[] { 1, 2, 3 };
    int[] j = new int[] { 1, 2, 4 };
    System.out.println(Arrays.equals(i, j));
  }
}





Arrays: equals(long[] a, long[] a2)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b = Arrays.equals(new long[] { 0L }, new long[] { 0L }); // true
  }
}





Arrays: equals(Object[] a, Object[] a2)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    String[] abc = { "a", "b", "c", "d" };
    String[] xyz = { "A", "b", "c", "s" };
    String[] java = { "Java", "Dot", "Com" };
    System.out.println(Arrays.equals(abc, xyz));
    System.out.println(Arrays.equals(abc, java));
  }
}





Arrays: equals(short[] a, short[] a2)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean b = Arrays.equals(new short[] { 0 }, new short[] { 0 }); // true
  }
}





Arrays: fill(boolean[] a, boolean val)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    boolean[] booleanArr = new boolean[10];
    boolean booleanFillValue = false;
    Arrays.fill(booleanArr, booleanFillValue);
  }
}





Arrays: fill(boolean[] a, int fromIndex, int toIndex, boolean val)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    int startIndex = 0;
    int endIndex = 4;
    boolean[] booleanArr = new boolean[10];
    boolean booleanFillValue = true;
    Arrays.fill(booleanArr, startIndex, endIndex, booleanFillValue);
  }
}





Arrays: fill(byte[] a, byte val)

   
import java.util.Arrays;
public class Main {
  public static void main(String[] a) {
    byte array[] = new byte[10];
//    Arrays.fill(array, 4); // illegal
    for (int i : array) {
      System.out.println(i);
    }
    Arrays.fill(array, (byte) 4); // Okay
    for (int i : array) {
      System.out.println(i);
    }
  }
}





Arrays: fill(byte[] a, int fromIndex, int toIndex, byte val)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    int startIndex = 0;
    int endIndex = 4;
    byte[] byteArr = new byte[10];
    byte byteFillValue = 1;
    Arrays.fill(byteArr, startIndex, endIndex, byteFillValue);
  }
}





Arrays: fill(char[] a, char val)

   
public class Main{
  public static void main(String[] arg){
     char[] message = new char[50];
     java.util.Arrays.fill(message, "A");
     for(char ch: message){
       System.out.println(ch);
     }
  }
}





Arrays: fill(char[] a, int fromIndex, int toIndex, char val)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    int startIndex = 0;
    int endIndex = 4;
    char[] charArr = new char[10];
    char charFillValue = 1;
    Arrays.fill(charArr, startIndex, endIndex, charFillValue);
  }
}





Arrays: fill(double[] a, double val)

   

import java.util.Arrays;
public class Main {
  public static void main(String[] arg) {
    double[] data = new double[50]; // An array of 50 values of type double
    Arrays.fill(data, 1.0);                     // Fill all elements of data with 1.0
    
    for (double d: data) { 
      System.out.println(d);
    }
    
  }
}





Arrays: fill(double[] a, int fromIndex, int toIndex, double val)

  
 
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    int startIndex = 0;
    int endIndex = 4;
    double[] doubleArr = new double[10];
    double doubleFillValue = 1;
    Arrays.fill(doubleArr, startIndex, endIndex, doubleFillValue);
  }
}





Arrays: fill(float[] a, float val)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    float[] floatArr = new float[10];
    float floatFillValue = -1;
    Arrays.fill(floatArr, floatFillValue);
  }
}





Arrays: fill(float[] a, int fromIndex, int toIndex, float val)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    int startIndex = 0;
    int endIndex = 4;
    float[] floatArr = new float[10];
    float floatFillValue = 1;
    Arrays.fill(floatArr, startIndex, endIndex, floatFillValue);
  }
}





Arrays: fill(int[] a,int fromIndex,int toIndex,int val)

   
/**
 *Output: 
Original contents: 0 3 6 9 12 15 18 21 24 27 
Sorted: 0 3 6 9 12 15 18 21 24 27 
After fill(): 0 3 -1 -1 -1 -1 18 21 24 27 
After sorting again: -1 -1 -1 -1 0 3 18 21 24 27 
The value 9 is at location -7
 */
import java.util.Arrays;
public class MainClass {
  public static void main(String args[]) {
    int array[] = new int[10];
    for (int i = 0; i < 10; i++)
      array[i] = 3 * i;
    System.out.print("Original contents: ");
    display(array);
    Arrays.sort(array);
    System.out.print("Sorted: ");
    display(array);
    Arrays.fill(array, 2, 6, -1);
    System.out.print("After fill(): ");
    display(array);
    Arrays.sort(array);
    System.out.print("After sorting again: ");
    display(array);
    System.out.print("The value 9 is at location ");
    int index = Arrays.binarySearch(array, 9);
    System.out.println(index);
  }
  static void display(int array[]) {
    for (int i : array) {
      System.out.print(i + " ");
    }
    System.out.println();
  }
}





Arrays: fill(int[] a, int val)

   
import java.util.Arrays;
public class Main {
  public static void main(String[] a) {
    int array[] = new int[10];
    Arrays.fill(array, 100);
    for(int i: array){
      System.out.println(i);
    }
    Arrays.fill(array, 3, 6, 50);
    for(int i: array){
      System.out.println(i);
    }
  }
}





Arrays: fill(long[] a, int fromIndex, int toIndex, long val)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    int startIndex = 0;
    int endIndex = 4;
    long[] longArr = new long[10];
    long longFillValue = 1;
    Arrays.fill(longArr, startIndex, endIndex, longFillValue);
  }
}





Arrays: fill(long[] a, long val)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    long[] longArr = new long[10];
    long longFillValue = -1;
    Arrays.fill(longArr, longFillValue);
  }
}





Arrays: fill(Object[] a, int fromIndex, int toIndex, Object val)

   
import java.util.Arrays;
class Person implements Comparable<Person> {
  public Person(String firstName, String surname) {
    this.firstName = firstName;
    this.surname = surname;
  }
  public String toString() {
    return firstName + " " + surname;
  }
  public int compareTo(Person person) {
    int result = surname.rupareTo(person.surname);
    return result == 0 ? firstName.rupareTo(((Person) person).firstName) : result;
  }
  private String firstName;
  private String surname;
}
public class Main {
  public static void main(String[] a) {
    Person[] people = new Person[100];
    Arrays.fill(people, 0, 50, new Person("A", "B"));
    Arrays.fill(people, 50, 100, new Person("C", "D"));
    for (Person person : people) {
      System.out.println(person);
    }
  }
}





Arrays: fill(Object[] a, Object val)

  
import java.util.Arrays;
import java.util.Date;
public class Main {
  public static void main(String args[]) {
    int array[] = new int[10];
    Arrays.fill(array, 100);
    for (int i=0, n=array.length; i<n; i++) {
      System.out.println(array[i]);
    }
    System.out.println();
    Arrays.fill(array, 3, 6, 50);
    for (int i=0, n=array.length; i<n; i++) {
      System.out.println(array[i]);
    }
    byte array2[] = new byte[10];
    Arrays.fill(array2, (byte)4);
    System.out.println();
    Date array3[] = new Date[10];
    Arrays.fill(array3, "Help");
  }
}





Arrays: fill(short[] a, int fromIndex, int toIndex, short val)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    int startIndex = 0;
    int endIndex = 4;
    short[] shortArr = new short[10];
    short shortFillValue = 1;
    Arrays.fill(shortArr, startIndex, endIndex, shortFillValue);
  }
}





Arrays: fill(short[] a, short val)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] argv) throws Exception {
    short[] shortArr = new short[10];
    short shortFillValue = 2;
    Arrays.fill(shortArr, shortFillValue);
  }
}





Arrays: sort(byte[] a)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    byte[] b1 = new byte[] { 3, 2, 5, 4, 1 };
    for (byte b: b1){
      System.out.println(b);
    }
    Arrays.sort(b1);
    for (byte b: b1){
      System.out.println(b);
    }
    byte[] b2 = new byte[] { 5, 2, 3, 1, 4 };
    Arrays.sort(b2, 1, 4);
    for (byte b: b2){
      System.out.println(b);
    }
  }
}





Arrays: sort(byte[] a, int fromIndex, int toIndex)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    byte[] b1 = new byte[] { 3, 2, 5, 4, 1 };
    for (byte b: b1){
      System.out.println(b);
    }
    Arrays.sort(b1);
    for (byte b: b1){
      System.out.println(b);
    }
    byte[] b2 = new byte[] { 5, 2, 3, 1, 4 };
    Arrays.sort(b2, 1, 4);
    for (byte b: b2){
      System.out.println(b);
    }
  }
}





Arrays: sort(char[] a)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    char[] c1 = new char[] { "c", "h", "a", "r", "s" };
    for (char ch: c1){
      System.out.print(ch);
    }
    Arrays.sort(c1);
    for (char ch: c1){
      System.out.print(ch);
    }
    char[] c2 = new char[] { "c", "h", "a", "r", "s" };
    Arrays.sort(c2, 1, 4);
    for (char ch: c1){
      System.out.print(ch);
    }
  }
}





Arrays: sort(char[] a, int fromIndex, int toIndex)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    char[] c1 = new char[] { "c", "h", "a", "r", "s" };
    for (char ch: c1){
      System.out.print(ch);
    }
    Arrays.sort(c1);
    for (char ch: c1){
      System.out.print(ch);
    }
    char[] c2 = new char[] { "c", "h", "a", "r", "s" };
    Arrays.sort(c2, 1, 4);
    for (char ch: c1){
      System.out.print(ch);
    }
  }
}





Arrays: sort(double[] a)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    double[] d1 = new double[] { 3.1, 2.1, 5.1, 4.1, 1.1 };
    for (double d: d1){
      System.out.print(" " +d);
    }
    Arrays.sort(d1);
    for (double d: d1){
      System.out.print(" " +d);
    }
 
    double[] d2 = new double[] { 5, 2, 3, 1, 4 };
    Arrays.sort(d2, 1, 4);
    for (double d: d2){
      System.out.print(" " +d);
    } 
  }
}





Arrays: sort(double[] a, int fromIndex, int toIndex)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    double[] d1 = new double[] { 3.1, 2.1, 5.1, 4.1, 1.1 };
    for (double d: d1){
      System.out.print(" " +d);
    }
    Arrays.sort(d1);
    for (double d: d1){
      System.out.print(" " +d);
    }
 
    double[] d2 = new double[] { 5, 2, 3, 1, 4 };
    Arrays.sort(d2, 1, 4);
    for (double d: d2){
      System.out.print(" " +d);
    } 
  }
}





Arrays: sort(float[] a)

  

import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    float[] f1 = new float[] { 3.1f, 2.1f, 5.1f, 4.1f, 1.1f };
    for (float f: f1){
      System.out.print(" " + f);
    }
    Arrays.sort(f1);
    for (float f: f1){
      System.out.print(" " + f);
    }
    float[] f2 = new float[] { 5.1f, 2.1f, 3.1f, 1.1f, 4.1f };
    Arrays.sort(f2, 1, 4);
    for (float f: f2){
      System.out.print(" " + f);
    }
  }
}





Arrays: sort(float[] a, int fromIndex, int toIndex)

  
import java.util.Arrays;
public class Main {
  public static void main(String[] args) {
    float[] f1 = new float[] { 3.1f, 2.1f, 5.1f, 4.1f, 1.1f };
    for (float f: f1){
      System.out.print(" " + f);
    }
    Arrays.sort(f1);
    for (float f: f1){
      System.out.print(" " + f);
    }
    float[] f2 = new float[] { 5.1f, 2.1f, 3.1f, 1.1f, 4.1f };
    Arrays.sort(f2, 1, 4);
    for (float f: f2){
      System.out.print(" " + f);
    }
  }
}





Arrays: sort(in[] a)

   
/**
 *Output: 
Original contents: 0 3 6 9 12 15 18 21 24 27 
Sorted: 0 3 6 9 12 15 18 21 24 27 
After fill(): 0 3 -1 -1 -1 -1 18 21 24 27 
After sorting again: -1 -1 -1 -1 0 3 18 21 24 27 
The value 9 is at location -7
 */
import java.util.Arrays;
public class MainClass {
  public static void main(String args[]) {
    int array[] = new int[10];
    for (int i = 0; i < 10; i++)
      array[i] = 3 * i;
    System.out.print("Original contents: ");
    display(array);
    Arrays.sort(array);
    System.out.print("Sorted: ");
    display(array);
    Arrays.fill(array, 2, 6, -1);
    System.out.print("After fill(): ");
    display(array);
    Arrays.sort(array);
    System.out.print("After sorting again: ");
    display(array);
    System.out.print("The value 9 is at location ");
    int index = Arrays.binarySearch(array, 9);
    System.out.println(index);
  }
  static void display(int array[]) {
    for (int i : array) {
      System.out.print(i + " ");
    }
    System.out.println();
  }
}





Arrays: sort(T[] a, Comparator c)

   
/*
 Output:
c
b
a

 * */
import java.util.Arrays;
import java.util.Collections;
import java.util.ruparator;
public class MainClass {
  public static void main(String args[]) throws Exception {
    Comparator comp = Collections.reverseOrder();
    String[] a = new String[] { "a", "b", "c" };
    Arrays.sort(a, comp);
    for (int i = 0, n = a.length; i < n; i++) {
      System.out.println(a[i]);
    }
  }
}





Arrays: toString(Object[] a)

   
import java.util.Arrays;
public class Main {
  public static void main(String args[]) {
    System.out.printf("Before (original)\t%s%n", Arrays.toString(args));
    String copy[] = Arrays.copyOf(args, 4);
    System.out.printf("Before (copy)\t\t%s%n", Arrays.toString(copy));
    copy[0] = "A";
    copy[1] = "B";
    copy[2] = "C";
    copy[3] = "D";
    System.out.printf("After (original)\t%s%n", Arrays.toString(args));
    System.out.printf("After (copy)\t\t%s%n", Arrays.toString(copy));
  }
}