Showing posts with label ArrayList. Show all posts
Showing posts with label ArrayList. Show all posts

Tuesday, October 20, 2009

JSP - use directives to import java libraries

<%@ page import="java.util.*" %>
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP Page</title> 
    </head> 
    <body> 
 
        <% 
        ArrayList ll = new ArrayList();
 
            for (int i=0; i<100; i++) {
                ll.add(i);
            }
        
        %> 
 
        <h1>ArrayList size: <%= ll.size() %> </h1> 
        <h1>Location of item 77: <%= ll.indexOf(77) %> </h1> 
 
    </body> 
</html> 
 
 

Thursday, September 24, 2009

Java - how to create and use an ArrayList

import java.util.*;
 
// related posts:
// old (but fast) school array
// Thread safe vectors
// 
// 


public class Main {
 
    public static void main(String[] args) {
 
        // arraylists do not require a size definition 
        // when you declare or instantiate. 
        // you can add to your hearts content 
        ArrayList al = new ArrayList();
 
        al.add("number one");
 
        // display the size of the ArrayList 
        System.out.println(al.size());
        // indicates:  1 
 
        // fill up with 10 
        for (int i=0; i<10; i++){
            // you'll notice that it just appends to 
            // the existing list.  "number one" remains 
            // and all the numbers are tacked on after 
            al.add(i);
        }
 
        // display all ten with foreach 
        //for (Object o:al 
        for (int i=0; i<al.size(); i++){
            System.out.println(al.get(i) + " is type " +
                    al.get(i).getClass().toString());
        }
 
        // you'll notice from the output that a Java ArrayList 
        // can have a mixture of object types.  Here we 
        // blend Integer and String 
    }
}