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 arg[]) throws Exception {
    String xmlRecords = "<root><x>1</x><x>2</x><x>3</x><x>4</x></root>";
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = db.parse(is);/*from  w  w  w.j  a v a2  s.  c  o m*/
    NodeList nodes = doc.getElementsByTagName("x");
    System.out.println(nodes.getLength());
    List<String> valueList = new ArrayList<String>();
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        String name = element.getTextContent();
        // Element line = (Element) name.item(0);
        System.out.println("Name: " + name);
        valueList.add(name);
    }
}

From source file:DiagnosticDemo.java

public static void main(String[] args) {
    String sourceFile = "c:/HelloWorld.Java";
    JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager =

            compiler.getStandardFileManager(diagnostics);

    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.run();//w  w  w. j  a va2 s  .co  m
    try {
        fileManager.close();
    } catch (IOException e) {
    }
    List<Diagnostic<? extends JavaFileObject>> diagnosticList = diagnostics.getDiagnostics();
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticList) {
        System.out.println("Position:" + diagnostic.getStartPosition());
    }
}

From source file:com.sankalp.characterreader.CharacterReader.java

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

    double learningRate = 0.55;
    List<NeuralNetwork.Layer> hiddenLayerList = new ArrayList();
    hiddenLayerList.add(new NeuralNetwork.Layer(50));
    NeuralNetwork neuralNetwork = new NeuralNetwork(new NeuralNetwork.Layer(28 * 28),
            new NeuralNetwork.Layer(10), hiddenLayerList, learningRate);

    int trainingSampleCount = 60000;
    TrainingData trainingData = new TrainingData(new File("/home/sankalpkulshrestha/mnist/train-images/"),
            trainingSampleCount, new File("/home/sankalpkulshrestha/mnist/train-labels.csv"));

    int totalEpochs = 30;
    for (int index = 0; index < totalEpochs; index++) {
        System.out.println("---------- EPOCH " + index + " ----------");
        neuralNetwork.train(trainingData);

        TrainingData testData = new TrainingData(new File("/home/sankalpkulshrestha/mnist/test-images/"), 10000,
                new File("/home/sankalpkulshrestha/mnist/test-labels.csv"));

        System.out.println("Training over");
        double accuracy = neuralNetwork.measureAccuracy(testData);
        System.out.println(accuracy);
        if (index < totalEpochs - 1) {
            trainingData.shuffle();//from  ww w  .  j  av  a  2 s  .c  om
            trainingData.reset();
        }
    }
    System.out.println("Model trained. Enter image file names:");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int number = -1;
    List<String> expectedOutputList = FileUtils
            .readLines(new File("/home/sankalpkulshrestha/mnist/test-labels.csv"), "UTF-8");
    while ((number = Integer.parseInt(br.readLine())) != -1) {
        int output = neuralNetwork.test(new TrainingData.Sample(
                new File("/home/sankalpkulshrestha/mnist/test-images/" + number + ".jpg"), 0));
        System.out.println(output);
    }
}

From source file:SortByHouseNo.java

public static void main(String[] args) {
    String houseList[] = { "9-11", "9-01", "10-02", "10-01", "2-09", "3-88", "9-03", "9-3" };
    HouseNo house = null;//w  w  w.j  a v  a 2s  .  c  o  m
    ArrayList<HouseNo> sortedList = new ArrayList<>();
    for (String string : houseList) {
        String h = string.substring(0, string.indexOf('-'));
        String b = string.substring(string.indexOf('-') + 1);
        house = new HouseNo(h, b);
        sortedList.add(house);
    }

    System.out.println("Before Sorting :: ");
    for (HouseNo houseNo : sortedList) {
        System.out.println(houseNo);
    }

    Collections.sort(sortedList, new SortByHouseNo());
    System.out.println("\n\nAfter Sorting HouseNo :: ");
    for (HouseNo houseNo : sortedList) {
        System.out.println(houseNo);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newFactory();

    FileInputStream xml = new FileInputStream("input.xml");
    XMLStreamReader xsr = xif.createXMLStreamReader(xml);
    xsr.nextTag(); // Advance to "Persons" tag
    xsr.nextTag(); // Advance to "Person" tag

    JAXBContext jc = JAXBContext.newInstance(Person.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    List<Person> persons = new ArrayList<Person>();
    while (xsr.hasNext() && xsr.isStartElement()) {
        Person person = (Person) unmarshaller.unmarshal(xsr);
        persons.add(person);//from ww  w.ja  v a2 s .  co  m
        xsr.nextTag();
    }

    for (Person person : persons) {
        System.out.println(person.getName());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse("server.xml");
    Element root = document.getDocumentElement();
    Element rootElement = document.getDocumentElement();

    Collection<Server> svr = new ArrayList<Server>();
    svr.add(new Server());

    for (Server i : svr) {
        Element server = document.createElement("server");
        rootElement.appendChild(server);

        Element name = document.createElement("name");
        name.appendChild(document.createTextNode(i.getName()));
        server.appendChild(name);/*from  w  ww.j  a  v  a 2  s .co m*/

        Element port = document.createElement("port");
        port.appendChild(document.createTextNode(Integer.toString(i.getPort())));
        server.appendChild(port);

        root.appendChild(server);
    }

    DOMSource source = new DOMSource(document);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StreamResult result = new StreamResult("server.xml");
    transformer.transform(source, result);
}

From source file:Main.java

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

    Class.forName(DRIVER);// www  .  j a v a2 s .c  om
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT * FROM users");

    ResultSetMetaData metadata = resultSet.getMetaData();
    int columnCount = metadata.getColumnCount();

    ArrayList<String> columns = new ArrayList<String>();
    for (int i = 1; i < columnCount; i++) {
        String columnName = metadata.getColumnName(i);
        columns.add(columnName);
    }

    while (resultSet.next()) {
        for (String columnName : columns) {
            String value = resultSet.getString(columnName);
            System.out.println(columnName + " = " + value);
        }
    }
}

From source file:House.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    House house = new House();
    List animals = new ArrayList();
    animals.add(new Animal("Bosco the dog", house));
    animals.add(new Animal("Ralph the hamster", house));
    animals.add(new Animal("Fronk the cat", house));
    System.out.println("animals: " + animals);
    ByteArrayOutputStream buf1 = new ByteArrayOutputStream();
    ObjectOutputStream o1 = new ObjectOutputStream(buf1);
    o1.writeObject(animals);//from w w w .java  2  s.com
    o1.writeObject(animals); // Write a 2nd set
    // Write to a different stream:
    ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
    ObjectOutputStream o2 = new ObjectOutputStream(buf2);
    o2.writeObject(animals);
    // Now get them back:
    ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray()));
    ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray()));
    List animals1 = (List) in1.readObject(), animals2 = (List) in1.readObject(),
            animals3 = (List) in2.readObject();
    System.out.println("animals1: " + animals1);
    System.out.println("animals2: " + animals2);
    System.out.println("animals3: " + animals3);
}

From source file:Customer.java

public static void main(String[] args) {
    List<Customer> customers = new ArrayList<>();
    customers.add(new Customer("A", "a@gmail.com"));
    customers.add(new Customer("B", "b@gmail.com"));
    System.out.println("customers before email change - start");
    System.out.println(Customer.toString(customers));
    System.out.println("end");
    customers.get(1).setEmail("new.email@gmail.com");
    System.out.println("customers after email change - start");
    System.out.println(Customer.toString(customers));
    System.out.println("end");
}

From source file:contractEditor.SPConfFileEditor.java

public static void main(String[] args) {

    JSONObject obj = new JSONObject();
    ArrayList fileList = new ArrayList();

    fileList.add("confSP" + File.separator + "HOST1.json");
    fileList.add("confSP" + File.separator + "HOST2.json");
    fileList.add("confSP" + File.separator + "HOST3.json");
    fileList.add("confSP" + File.separator + "HOST4.json");

    obj.put("contract_list_of_SP", fileList);

    try {/*from  w w w.ja va  2 s  .  com*/

        FileWriter file = new FileWriter("confSP" + File.separator + "contractFileList.json");
        file.write(obj.toJSONString());
        file.flush();
        file.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.print(obj);

}