line.java

来自「程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件」· Java 代码 · 共 52 行

JAVA
52
字号
package examples.collections;

import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import java.io.PrintStream;
import java.awt.Point;

/** A class to demonstrate the use of the ArrayList
  * and Iterator classes in the java.util package
  */
public class Line {

   private List points = new ArrayList();

   /** Set the starting point for a line
     * @param p the starting point
     */
   public void setStart( Point p ) {
      points.clear();
      points.add( p );
   }

   /** Set the next point in a line
     * @param p the next point
     */
   public void addPoint( Point p ) {
      points.add( p );
   }

   /** Print all the points in a line
     * @param ps the stream where the points
     *           will be printed
     */
   public void listPoints( PrintStream ps ) {
      ListIterator li = points.listIterator();
      while ( li.hasNext() ) {
         ps.println( li.next() );
      }
   }

   /** Test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      Line x = new Line();
      x.setStart( new Point( 4, 11 ) );
      x.addPoint( new Point( -6, 1 ) );
      x.addPoint( new Point( 2, 3 ) );
      x.listPoints( System.out );
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?