Example usage for java.util ArrayList ArrayList

List of usage examples for java.util ArrayList ArrayList

Introduction

In this page you can find the example usage for java.util ArrayList ArrayList.

Prototype

public ArrayList() 

Source Link

Document

Constructs an empty list with an initial capacity of ten.

Usage

From source file:SafeListCopy.java

public static void main(String[] args) {
    List wordList = Collections.synchronizedList(new ArrayList());

    wordList.add("Synchronization");
    wordList.add("is");
    wordList.add("important");

    String[] wordA = (String[]) wordList.toArray(new String[0]);

    printWords(wordA);//  w  w  w.  ja v  a2  s .c om

    String[] wordB;
    synchronized (wordList) {
        int size = wordList.size();
        wordB = new String[size];
        wordList.toArray(wordB);
    }

    printWords(wordB);

    // Third technique (the 'synchronized' *is* necessary)
    String[] wordC;
    synchronized (wordList) {
        wordC = (String[]) wordList.toArray(new String[wordList.size()]);
    }

    printWords(wordC);
}

From source file:CollectionTest.java

public static void main(String[] args) {

    nLoops = 10000;// w ww.  j a v a2s  . c om
    doTest(new Vector());
    doTest(new ArrayList());
    doTest(Collections.synchronizedList(new ArrayList()));
    nLoops = Integer.parseInt(args[0]);

    System.out.println("Starting synchronized vector test");
    cleanGC();
    Timestamp syncTS = new Timestamp();
    doTest(new Vector());
    syncTS.stop();
    System.out.println("Synchronized vector took " + syncTS);

    System.out.println("Starting unsynchronized vector test");
    cleanGC();
    Timestamp unsyncTS = new Timestamp();
    unsyncTS.stop();
    System.out.println("Unsynchronized vector took " + unsyncTS);

    double d = ((double) (syncTS.elapsedTime() - unsyncTS.elapsedTime())) / nLoops;
    System.out.println("Unsynchronized operation saves " + d + " " + syncTS.units() + " per call");

    System.out.println("Starting synchronized array list test");
    cleanGC();
    syncTS = new Timestamp();
    doTest(Collections.synchronizedList(new ArrayList()));
    syncTS.stop();
    System.out.println("Synchronized array list took " + syncTS);

    System.out.println("Starting unsynchronized array list test");
    cleanGC();
    unsyncTS = new Timestamp();
    doTest(new ArrayList());
    unsyncTS.stop();
    System.out.println("Unsynchronized aray list took " + unsyncTS);

    d = ((double) (syncTS.elapsedTime() - unsyncTS.elapsedTime())) / nLoops;
    System.out.println("Unsynchronized operation saves " + d + " " + syncTS.units() + " per call");
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    List mylist = new ArrayList();
    for (int i = 0; i < args.length; i++) {
        FileInputStream in = new FileInputStream(args[i]);
        Certificate c = cf.generateCertificate(in);
        mylist.add(c);/*from  w ww  . j  av a 2 s .  c om*/
    }
    CertStoreParameters cparam = new CollectionCertStoreParameters(mylist);
    CertStore cs = CertStore.getInstance("Collection", cparam);
    System.out.println(cs.getCertStoreParameters());
    System.out.println(cs.getProvider());
    System.out.println(cs.getType());

}

From source file:FileDialogMultipleFileNameSelection.java

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);

    FileDialog dlg = new FileDialog(shell, SWT.MULTI);
    Collection files = new ArrayList();
    if (dlg.open() != null) {
        String[] names = dlg.getFileNames();
        for (int i = 0, n = names.length; i < n; i++) {
            StringBuffer buf = new StringBuffer(dlg.getFilterPath());
            if (buf.charAt(buf.length() - 1) != File.separatorChar)
                buf.append(File.separatorChar);
            buf.append(names[i]);/* ww  w . j  a  va2s.  c  o m*/
            files.add(buf.toString());
        }
    }
    System.out.println(files);
    display.dispose();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Car> carsList = new ArrayList<Car>();
    Set<Car> carsset = new HashSet<Car>();
    File fXmlFile = new File("cars.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();
    NodeList nList = doc.getElementsByTagName("cars");
    Car tempCar = null;/*from  ww w  .j a v a  2  s  . c  om*/
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            tempCar = new Car();
            Element eElement = (Element) nNode;
            tempCar.setModel(eElement.getElementsByTagName("model").item(0).getTextContent());
            tempCar.setVersion(eElement.getElementsByTagName("version").item(0).getTextContent());
            carsList.add(tempCar);
            carsset.add(tempCar);

        }
    }
    for (Car cs : carsset) {
        int count = 0;
        for (Car cl : carsList) {
            if (cs.equals(cl)) {
                count = count + 1;
            }
        }
        System.out.println(cs + "\t" + count);
    }
}

From source file:Synchronization.java

public static void main(String[] args) {
    Collection c = Collections.synchronizedCollection(new ArrayList());
    List list = Collections.synchronizedList(new ArrayList());
    Set s = Collections.synchronizedSet(new HashSet());
    Map m = Collections.synchronizedMap(new HashMap());
}

From source file:Main.java

public static void main(String[] args) {

    Optional<String> value = Optional.of("some value");
    System.out.println(value.isPresent());
    System.out.println(value.get());
    String str = null;/*from www.  j ava 2s  .  co m*/
    // Optional.of(str);

    Optional<Integer> o = Optional.empty();
    System.out.println(o.isPresent());
    System.out.println(o.orElse(42));

    List<Integer> results = new ArrayList<>();
    Optional<Integer> second = Optional.of(3);
    second.ifPresent(results::add); // must operate via side-effects,
                                    // unfortunately...
    System.out.println(results);

    o = Optional.empty();
    System.out.println(o.orElse(42));

    o = Optional.of(42);
    System.out.println(o.get());

    o = Optional.empty();
    o.get();
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // create a new collection and add some elements
    Collection collection = new ArrayList();
    collection.add("One");
    collection.add("Two");
    collection.add("Three");

    // append the collection in the LinkedList
    list.addAll(collection);//from w  w w  .ja  va 2s.c o m

    // print the new list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // create a new collection and add some elements
    Collection collection = new ArrayList();
    collection.add("One");
    collection.add("Two");
    collection.add("Three");

    // add the collection in the LinkedList at index 2
    list.addAll(2, collection);/*from   ww  w  .  ja  v a2s.  c  o  m*/

    // print the new list
    System.out.println("LinkedList:" + list);
}

From source file:Batch.java

static public void main(String[] args) {
    Connection conn = null;//from ww w  .  j  a v a  2s  . c om

    try {
        ArrayList breakable = new ArrayList();
        PreparedStatement stmt;
        Iterator users;
        ResultSet rs;

        Class.forName(args[0]).newInstance();
        conn = DriverManager.getConnection(args[1], args[2], args[3]);
        stmt = conn.prepareStatement("SELECT user_id, password " + "FROM user");
        rs = stmt.executeQuery();
        while (rs.next()) {
            String uid = rs.getString(1);
            String pw = rs.getString(2);

            // Assume PasswordCracker is some class that provides
            // a single static method called crack() that attempts
            // to run password cracking routines on the password
            //                if( PasswordCracker.crack(uid, pw) ) {
            //                  breakable.add(uid);
            //            }
        }
        stmt.close();
        if (breakable.size() < 1) {
            return;
        }
        stmt = conn.prepareStatement("UPDATE user " + "SET bad_password = 'Y' " + "WHERE uid = ?");
        users = breakable.iterator();
        while (users.hasNext()) {
            String uid = (String) users.next();

            stmt.setString(1, uid);
            stmt.addBatch();
        }
        stmt.executeBatch();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }
    }
}