Warning: Undefined variable $mode in /var/www/virtual/qbnz.com/htdocs/highlighter/examples/java/array-set.php on line 6
GeSHi - Generic Syntax Highlighter :: Examples
 
Navigation
Home News Examples Demo Downloads FAQ Documentation Mailing Lists License
Support GeSHi!
If you're using GeSHi, why not help GeSHi out? You can link to GeSHi with this image:
Powered by GeSHi
Get the HTML

Project Status
The latest stable version of GeSHi is 1.0.8.11, released on the 19th of Aug, 2012.

Supported Languages:
*ABAP
*Actionscript
*ADA
*Apache Log
*AppleScript
*APT sources.list
*ASM (m68k)
*ASM (pic16)
*ASM (x86)
*ASM (z80)
*ASP
*AutoIT
*Backus-Naur form
*Bash
*Basic4GL
*BlitzBasic
*Brainfuck
*C
*C for Macs
*C#
*C++
*C++ (with QT)
*CAD DCL
*CadLisp
*CFDG
*CIL / MSIL
*COBOL
*ColdFusion
*CSS
*D
*Delphi
*Diff File Format
*DIV
*DOS
*DOT language
*Eiffel
*Fortran
*FourJ's Genero
*FreeBasic
*GetText
*glSlang
*GML
*gnuplot
*Groovy
*Haskell
*HQ9+
*HTML
*INI (Config Files)
*Inno
*INTERCAL
*IO
*Java
*Java 5
*Javascript
*KiXtart
*KLone C & C++
*LaTeX
*Lisp
*LOLcode
*LotusScript
*LScript
*Lua
*Make
*mIRC
*MXML
*MySQL
*NSIS
*Objective C
*OCaml
*OpenOffice BASIC
*Oracle 8 & 11 SQL
*Pascal
*Perl
*PHP
*Pixel Bender
*PL/SQL
*POV-Ray
*PowerShell
*Progress (OpenEdge ABL)
*Prolog
*ProvideX
*Python
*Q(uick)BASIC
*robots.txt
*Ruby
*Ruby on Rails
*SAS
*Scala
*Scheme
*Scilab
*SDLBasic
*Smalltalk
*Smarty
*SQL
*T-SQL
*TCL
*thinBasic
*TypoScript
*Uno IDL
*VB.NET
*Verilog
*VHDL
*VIM Script
*Visual BASIC
*Visual Fox Pro
*Visual Prolog
*Whitespace
*Winbatch
*Windows Registry Files
*X++
*XML
*Xorg.conf

GeSHi 1.0.8.11 is the current stable release, with eighteen new languages and bug fixes over the last release.

GeSHi 1.1.2alpha5 is the current latest version from the development branch, with full C support (see the GeSHi development website).
Subscribe
RSS 2
Mailing Lists
HomeNewsExamplesDemoDownloadsFAQDocumentationMailing ListsLicense 
2:55 pm GMT

Examples

Examples » Java
import java.util.Enumeration;
import java.util.NoSuchElementException;
import jds.Set;
import jds.Bag;
import java.util.Enumeration;
 
/**
 * ArraySet - a Set collection;
 * @author Peter Andreae
 * @version July 2004
 * @see jds.Collection
 *
 * The implementation uses an array to store the items.
 * It does not keep the items in any particular order, and may change the order of
 *  the remaining items when removing items.
 * It does not allow null as an element of a set.
 * It is not particularly efficient
 * When full (like Vector) it will create a new array of double the current size, and
 *  copy all the items over to the new array
 */
 
public class ArraySet implements Set {
  // data areas
  private int elementCount = 0;
  private Object [ ] elementData = new Object[10];
 
  public ArraySet(){}
 
  // The Collection interface
  /** Determines whether the collection is empty
   * @return true if the collection is empty */
  public boolean isEmpty () {
    // YOUR CODE HERE
    return elementCount == 0;
    // END OF YOUR CODE
  }
 
  /** Determines number of elements in collection
   * @return number of elements in collection as integer */
  public int size () {
    // YOUR CODE HERE
    return elementCount;
    // END OF YOUR CODE
  }
 
  /** Returns an Enumeration for the collection
   */
  public Enumeration elements () {
    // YOUR CODE HERE
    return new ArraySetEnumeration(this);
    // END OF YOUR CODE
  }
 
  // the Bag Interface (Set extends Bag)
 
  /** Add an element to the set, but only if not present already
   * @param	the element */
  public void addElement (Object item) {
    // YOUR CODE HERE
    if (item!= null && !containsElement(item)){
      ensureCapacity();
      elementData[elementCount]=item;
      elementCount++;
    }
    // END OF YOUR CODE
  }
 
  /** Determine if the set contains an item
   * @param	the item to look for
   * @return	boolean
   */
  public boolean containsElement (Object item) {
    // YOUR CODE HERE
    if (item==null) return false;
    for (int i=0; i<elementCount; i++){
      if (item.equals(elementData[i]))
	return true;
    }
    return false;
    // END OF YOUR CODE
  }
 
 
  /** Returns the element in the set matching [equal()] a given item.
   * @param	the item to look for
   * @return	an item or null
   * Throws  NoSuchElementException if no such item.
   */
  public Object findElement (Object item) {
    // YOUR CODE HERE
    if (item==null) throw new NoSuchElementException();
    for (int i=0; i<elementCount; i++){
      if (item.equals(elementData[i]))
	return elementData[i];
    }
    throw new NoSuchElementException();
    // END OF YOUR CODE
  }
 
  /** Remove an element matching a given item
   * Makes no change to the set if the item is not present.
   * @param	the item to look for
   * Throws  NoSuchElementException if no such item.
   */
  public void removeElement (Object item) {
    // YOUR CODE HERE
    if (item==null) throw new NoSuchElementException();
    for (int i=0; i<elementCount; i++){
      if (item.equals(elementData[i])){
	elementCount--;
	elementData[i]= elementData[elementCount];
	return;
      }
    }
    throw new NoSuchElementException();
    // END OF YOUR CODE
  }
 
  // the rest of the Set Interface
 
 
  /** form union with argument set
   * @param other collection to be joined to current
   */
  public void unionWith (Bag other){
    // add each element of other to this (duplicates won't be added)
    // EXTENSION!!
    // YOUR CODE HERE
    for (Enumeration e = other.elements(); e.hasMoreElements(); ){
      addElement(e.nextElement());
    }
    // END OF YOUR CODE
  }
 
  /** form intersection with argument set
   * @param other collection to be intersected with current
   */
  public void intersectWith (Bag other){
    // EXTENSION!!
    // YOUR CODE HERE
    // Every item in other that is also in this, move to the front, then "drop off" the others
    int newCount = 0;  // number of items to keep because they are in both sets
    for (Enumeration e = other.elements(); e.hasMoreElements(); ){
      Object item = e.nextElement();
      for (int i=0; i<elementCount; i++){
	if (item.equals(elementData[i])){
	  //swap items at i and at newCount;
	  Object temp = elementData[i];
	  elementData[i] = elementData[newCount];
	  elementData[newCount] = temp;
	  newCount++;
	  break;
	}
      }
    }
    elementCount=newCount;
    // END OF YOUR CODE
  }
 
  /** form difference from argument set
   * @param aSet collection to be compared to current
   */
  public void differenceWith (Bag other){
    // EXTENSION!!
    // YOUR CODE HERE
    for (Enumeration e = other.elements(); e.hasMoreElements(); ){
      Object item = e.nextElement();
      // Remove item from this set if present
      for (int i=0; i<elementCount; i++){
	if (item.equals(elementData[i])){
	  elementCount--;
	  elementData[i]= elementData[elementCount];
	  break;
	}
      }
    }
    // END OF YOUR CODE
  }
 
  /** is current set a subset of another collecton
   * @param other collection to be tested against
   * @return true if current set is subset of argument collection
   */
  public boolean subsetOf (Bag other){
    // must check that each item of this is also in the other.
    // EXTENSION!!
    // YOUR CODE HERE
    // this implementation depends on other being a Bag.
    for (int i=0; i<elementCount; i++){
      if (!other.containsElement(elementData[i])){
	//item i is missing from other
	return false;
      }
    }
    // we didn't find any item that was missing from other
    return true;
    // END OF YOUR CODE
  }
 
  // ArraySet utility methods
  /**
   * ensure elementData array has sufficient number of elements
   * to add a new element
   *
   */
  private void ensureCapacity () {
    // YOUR CODE HERE
    if (elementCount < elementData.length) return;
    Object [ ] newArray = new Object[elementData.length*2];
    for (int i = 0; i < elementCount; i++)
      newArray[i] = elementData[i];
    elementData = newArray;
    // END OF YOUR CODE
  }
 
  private class ArraySetEnumeration implements Enumeration{
    // needs fields, constructor, hasMoreElements(), and nextElement()
    // YOUR CODE HERE
 
    private ArraySetEnumeration (ArraySet b) {
      data = b.elementData;
      size = b.elementCount;
    }
    private Object[] data;
    private int size;
    private int index = 0;
 
    /** Can the enumeration continue?
     * @return true if enumeration has at least one more element
     */
    public boolean hasMoreElements () {
      return index < size;
    }
 
    /** get next element in enumeration
     * @return value of next element in enumeration
     */
    public Object nextElement () {
      return data[index++];
    }
    // END OF YOUR CODE
  }
 
 
}

This category contains 2 examples, and has been viewed 50145 times.

  • Array Set - An example of the ArraySet ADT in Java (14954 views)
  • Simple counter - A simple Java example (12209 views)