<%@ 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>
Showing posts with label for. Show all posts
Showing posts with label for. Show all posts
Tuesday, October 20, 2009
JSP - use directives to import java libraries
Friday, September 25, 2009
Java - reverse the word order of a sentence
public class Main { public static void main(String[] args) { Words w = new Words(); String sentence = "The example sentence is here"; System.out.println(sentence); System.out.println("reversed to:"); System.out.println(w.reverseWordOrderOfSentence(sentence)); } } class Words{ public String reverseWordOrderOfSentence(String sentence){ // split up by words String[] words = sentence.split(" "); sentence = ""; // iterate backward through the array // rebuilding the sentence from the back forward for (int i=words.length; i>0; i--){ sentence += words[i-1]; sentence += " "; } // remove the trailing white space return sentence.trim(); } } /* * OUTPUT: The example sentence is here reversed to: here is sentence example The */
Java - how to create and use a LinkedList
import java.util.*; public class Main { public static void main(String[] args) { LinkedList ll = new LinkedList(); // add to linked list ll.add("first String object"); // total items in the linked list System.out.println(ll.size()); // indicates: 1 // the list is not type specific // Strings and ints can coexist ll.add(22); // add ten more items for (int i=0; i<10; i++){ ll.add(i); } String oType = ""; // iterate through all existing items // with for-each for(Object o:ll){ oType = o.getClass().toString(); System.out.println(o.toString() + ", is type: " + oType); } // remove items from list ll.removeFirstOccurrence(3); // remove the first item from the list ll.remove(); // remove the last item from the list ll.removeLast(); } }
Labels:
add,
for,
foreach,
getClass,
java,
LinkedList,
Object,
remove,
removeFirstOccurence,
removeLast
Thursday, September 24, 2009
Java - how to create and use a Vector
import java.util.*; public class Main { public static void main(String[] args) { Vector v = new Vector(); // The Vector class is almost identical to the // ArrayList class. The main difference is that // a Vector is thread safe. v.add("number one is a String"); v.add(22); // number two is an Integer for (int i=0; i<10; i++){ // add ten integers v.add(i); } // iterate through the vector with a foreach for (Object o:v){ System.out.println(o); } // as with ArrayLists you'll notice that // Vectors are not type specific. You can // put in whatever you want } }
Subscribe to:
Posts (Atom)