FileDocCategorySizeDatePackage
Array1.javaAPI DocExample3997Mon Apr 06 18:10:26 BST 1998c08.newcollections

Array1

public class Array1 extends Object

Fields Summary
static Random
r
static String
ssource
static char[]
src
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    byte[] b = new byte[15];
    r.nextBytes(b); // Fill with random bytes
    print(b);
    Arrays.sort(b);
    print(b);
    int loc = Arrays.binarySearch(b, b[10]);
    System.out.println("Location of " + b[10] +
      " = " + loc);
    // Test String sort & search:
    String[] s = randStrings(4, 10);
    print(s);
    Arrays.sort(s);
    print(s);
    loc = Arrays.binarySearch(s, s[4]);
    System.out.println("Location of " + s[4] +
      " = " + loc);
  
public static voidprint(byte[] b)

    for(int i = 0; i < b.length; i++)
      System.out.print(b[i] + " ");
    System.out.println();
  
public static voidprint(java.lang.String[] s)

    for(int i = 0; i < s.length; i++)
      System.out.print(s[i] + " ");
    System.out.println();
  
public static java.lang.StringrandString(int length)

  // Create a random String
       
    char[] buf = new char[length];
    int rnd;
    for(int i = 0; i < length; i++) {
      rnd = Math.abs(r.nextInt()) % src.length;
      buf[i] = src[rnd];
    }
    return new String(buf);
  
public static java.lang.String[]randStrings(int length, int size)

    String[] s = new String[size];
    for(int i = 0; i < size; i++)
      s[i] = randString(length);
    return s;