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:Main.java

public static void main(String[] args) throws Exception {
    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);

    Namespace ns1 = eventFactory.createNamespace("ns1", "http://www.e.com/ns1");
    Namespace ns2 = eventFactory.createNamespace("ns2", "http://www.e.com/ns2");
    List<Namespace> namespaceList = new ArrayList<Namespace>();
    namespaceList.add(ns1);/*from  ww w. ja v  a2 s  .co m*/
    namespaceList.add(ns2);

    Attribute attribute = eventFactory.createAttribute(ns2.getPrefix(), ns2.getNamespaceURI(), "attribute",
            "true");

    writer.add(eventFactory.createStartElement(ns1.getPrefix(), ns1.getNamespaceURI(), "sample",
            Collections.singletonList(attribute).iterator(), namespaceList.iterator()));
    writer.add(eventFactory.createEndDocument());
    writer.flush();
}

From source file:CollectionAll.java

public static void main(String[] args) {
    List list1 = new LinkedList();
    list1.add("list");
    list1.add("dup");
    list1.add("x");
    list1.add("dup");
    traverse(list1);/*from ww  w.  ja v a2 s .co m*/
    List list2 = new ArrayList();
    list2.add("list");
    list2.add("dup");
    list2.add("x");
    list2.add("dup");
    traverse(list2);
    Set set1 = new HashSet();
    set1.add("set");
    set1.add("dup");
    set1.add("x");
    set1.add("dup");
    traverse(set1);
    SortedSet set2 = new TreeSet();
    set2.add("set");
    set2.add("dup");
    set2.add("x");
    set2.add("dup");
    traverse(set2);
    LinkedHashSet set3 = new LinkedHashSet();
    set3.add("set");
    set3.add("dup");
    set3.add("x");
    set3.add("dup");
    traverse(set3);
    Map m1 = new HashMap();
    m1.put("map", "Java2s");
    m1.put("dup", "Kava2s");
    m1.put("x", "Mava2s");
    m1.put("dup", "Lava2s");
    traverse(m1.keySet());
    traverse(m1.values());
    SortedMap m2 = new TreeMap();
    m2.put("map", "Java2s");
    m2.put("dup", "Kava2s");
    m2.put("x", "Mava2s");
    m2.put("dup", "Lava2s");
    traverse(m2.keySet());
    traverse(m2.values());
    LinkedHashMap /* from String to String */ m3 = new LinkedHashMap();
    m3.put("map", "Java2s");
    m3.put("dup", "Kava2s");
    m3.put("x", "Mava2s");
    m3.put("dup", "Lava2s");
    traverse(m3.keySet());
    traverse(m3.values());
}

From source file:Student.java

public static void main(String[] args) {
    List<Student> al = new ArrayList<>();
    al.add(new Student("Z", 34.34));
    al.add(new Student("M", 123.22));
    al.add(new Student("A", 1378.00));
    al.add(new Student("D", 99.22));
    al.add(new Student("Q", -19.08));
    Collections.sort(al);//from w ww .j  ava 2  s .c o m
    System.out.println(al);
}

From source file:ID.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    ID id = new ID();
    List employees = new ArrayList();
    employees.add(new Employee("A", id));
    employees.add(new Employee("B", id));
    employees.add(new Employee("C", id));
    System.out.println("employees: " + employees);
    ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
    ObjectOutputStream o1 = new ObjectOutputStream(buf1);
    o1.writeObject(employees);//w ww . j  av  a  2s  .  c  o  m
    o1.writeObject(employees);

    ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
    ObjectOutputStream o2 = new ObjectOutputStream(buf2);
    o2.writeObject(employees);

    ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray()));
    ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));
    List emp1 = (List) in1.readObject(), emp2 = (List) in1.readObject(), emp3 = (List) in2.readObject();
    System.out.println("emp1: " + emp1);
    System.out.println("emp2: " + emp2);
    System.out.println("emp3: " + emp3);
}

From source file:HannonHillSecret.HannonHillSecret.java

/**
 * @param args the command line arguments
 *///from   w  w  w .  ja  va 2s. c o m
public static void main(String[] args) {
    // This is a placeholder for the provided value of N
    final int N = 55;
    int currentNum = 0;
    int x = 0;
    int sizePrimes;
    boolean isAdditive = true;
    ArrayList<Integer> primeNumbers = new ArrayList<>();

    currentNum = Primes.nextPrime(currentNum);

    //Add all prime numbers less than or equal to N
    while (currentNum <= N) {
        primeNumbers.add(currentNum);
        currentNum = Primes.nextPrime(currentNum++);
    }

    sizePrimes = primeNumbers.size();

    // If there are only two prime numbers in the arraylist, it means it is empty or there
    // is only one.
    if (sizePrimes < 2) {
        System.out.println("Cannot test if Secret is additive since there "
                + "are not two or more prime numbers less than N!");
    } else // Testing for additive property is possible
    {
        outerloop:
        // Assuming the additive test only requires pair combinations, go through
        // all possible pairs until all pass or one fails
        while (x < sizePrimes && isAdditive) {
            for (int y = x + 1; y <= sizePrimes; y++) {
                isAdditive = isSecretAdditive(primeNumbers.get(x), primeNumbers.get(x));

                //Failed additive test for a combination of prime numbers,
                // so break the while loop and return false
                if (!isAdditive) {
                    break outerloop;
                }
            }
            x++;
        }

        if (isAdditive) {
            System.out.println("Secret is additive!");
        } else {
            System.out.println("Secret is NOT additive!");
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String sourceFile = "c:/HelloWorld.Java";
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

    List<File> sourceFileList = new ArrayList<File>();
    sourceFileList.add(new File(sourceFile));
    Iterable<? extends JavaFileObject> compilationUnits = fileManager
            .getJavaFileObjectsFromFiles(sourceFileList);
    CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
    task.call();/*from   ww w.  ja  va  2s .com*/
    fileManager.close();
    List<Diagnostic<? extends JavaFileObject>> diagnosticList = diagnostics.getDiagnostics();
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) {
        System.out.println("Position:" + diagnostic.getStartPosition());
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory();
    SSLSocket socket = (SSLSocket) factory.createSocket("127.0.0.1", 9999);
    socket.startHandshake();// w ww.j  a  v  a  2s  . co  m
    SSLSession session = socket.getSession();
    java.security.cert.Certificate[] servercerts = session.getPeerCertificates();

    List mylist = new ArrayList();
    for (int i = 0; i < servercerts.length; i++) {
        mylist.add(servercerts[i]);
    }

    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    CertPath cp = cf.generateCertPath(mylist);

    FileOutputStream f = new FileOutputStream("CertPath.dat");
    ObjectOutputStream b = new ObjectOutputStream(f);
    b.writeObject(cp);

}

From source file:ListDemo.java

public static void main(String args[]) {
    // do timing for LinkedList
    System.out.println("time for LinkedList = " + timeList(new LinkedList()));

    // do timing for ArrayList
    System.out.println("time for ArrayList = " + timeList(new ArrayList()));
}

From source file:Card.java

public static void main(String args[]) {
    List<Card> lc = new ArrayList<Card>();
    lc.add(new Card(Card.Rank.SIX, Card.Suit.CLUBS));
    lc.add(new Card(Card.Rank.TEN, Card.Suit.CLUBS));
    lc.add(new Card(Card.Rank.SIX, Card.Suit.HEARTS));
    lc.add(new Card(Card.Rank.ACE, Card.Suit.HEARTS));

    System.out.println(lc);//w w  w . jav  a2s  . c  om
    Collections.sort(lc);
    System.out.println(lc);
}

From source file:SortByMark.java

public static void main(String args[]) {
    List<Student> students = new ArrayList<Student>();
    SortByMark sortByMark = new SortByMark();

    students.add(new Student("A", new Double(34.34)));
    students.add(new Student("C", new Double(123.22)));
    students.add(new Student("B", new Double(13.00)));
    students.add(new Student("Z", new Double(99.22)));
    students.add(new Student("X", new Double(-19.08)));

    Collections.sort(students, sortByMark);

    for (Student student : students) {
        System.out.println(student);
    }// www . j  a va  2s . co m
}