Java基础之集合框架——使用真的的链表LinkedList<>(TryPolyLine)
控制台程序。
public class Point {
// Create a point from its coordinates
public Point(double xVal, double yVal) {
x = xVal;
y = yVal;
}
// Create a point from another point
public Point(Point point) {
x = point.x;
y = point.y;
}
// Convert a point to a string
@Override
public String toString() {
return x+","+y;
}
// Coordinates of the point
protected double x;
protected double y;
}
import java.util.LinkedList;
public class PolyLine {
// Construct a polyline from an array of points
public PolyLine(Point[] points) {
// Add the points
for(Point point : points) {
polyline.add(point);
}
}
// Construct a polyline from an array of coordinates
public PolyLine(double[][] coords) {
for(double[] xy : coords) {
addPoint(xy[0], xy[1]);
}
}
// Add a Point object to the list
public void addPoint(Point point) {
polyline.add(point); // Add the new point
}
// Add a point to the list
public void addPoint(double x, double y) {
polyline.add(new Point(x, y));
}
// String representation of a polyline
@Override
public String toString() {
StringBuffer str = new StringBuffer("Polyline:");
for(Point point : polyline) {
str.append(" "+ point); // Append the current point
}
return str.toString();
}
private LinkedList<Point> polyline = new LinkedList<>();
}
public class TryPolyLine {
public static void main(String[] args) {
// Create an array of coordinate pairs
double[][] coords = { {1, 1}, {1, 2}, { 2, 3},
{-3, 5}, {-5, 1}, {0, 0} };
// Create a polyline from the coordinates and display it
PolyLine polygon = new PolyLine(coords);
System.out.println(polygon);
// Add a point and display the polyline again
polygon.addPoint(10, 10);
System.out.println(polygon);
// Create Point objects from the coordinate array
Point[] points = new Point[coords.length];
for(int i = 0; i < points.length; ++i)
points[i] = new Point(coords[i][0],coords[i][1]);
// Use the points to create a new polyline and display it
PolyLine newPoly = new PolyLine(points);
System.out.println(newPoly);
}
}
Java基础之集合框架——使用真的的链表LinkedList<>(TryPolyLine)的更多相关文章
- Java基础--说集合框架
版权所有,转载注明出处. 1,Java中,集合是什么?为什么会出现? 根据数学的定义,集合是一个元素或多个元素的构成,即集合一个装有元素的容器. Java中已经有数组这一装有元素的容器,为什么还要新建 ...
- 黑马程序员——【Java基础】——集合框架
---------- android培训.java培训.期待与您交流! ---------- 一.集合框架概述 (一)集合框架中集合类关系简化图 (二)为什么出现集合类? 面向对象语言对事物的体现都是 ...
- java基础37 集合框架工具类Collections和数组操作工具类Arrays
一.集合框架工具类:Collections 1.1.Collections类的特点 该工具类中所有的方法都是静态的 1.2.Collections类的常用方法 binarySearch(List< ...
- Thinking in java基础之集合框架(转载)
集合简介(容器)把具有相同性质的一类东西,汇聚成一个整体,就可以称为集合,例如这里有20个苹果,我们把每一个苹果当成一个东西(一个对象),然后我们借用袋子把这20个苹果装起来,而这个袋子就是集合(也叫 ...
- Java基础之集合框架(Collection接口和List接口)
首先我们说说集合有什么作用. 一.集合的作用 1.在类的内部,对数据进行组织: 2.简单而快速的搜索大数量的条目: 3.有的集合接口,提供一系列排列有序的元素,并且可以在序列中间快速的插入或者删除有关 ...
- java基础之集合框架
6.集合框架: (1)为什么出现集合类? 面向对象对事物的体现都是以对象的形式,为了方便对多个对象的操作,就对对象进行存储. 集合就是存储对象最常用的一种方式. (2)数组和集合都是容器,两者有何不同 ...
- Java基础之集合框架——使用堆栈Stack<>对象模拟发牌(TryDeal)
控制台程序. public enum Rank { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, A ...
- Java基础之集合框架——使用集合Vector<>挑选演员(TryVector)
控制台程序. public class Person implements Comparable<Person> { // Constructor public Person(String ...
- Java基础之集合框架类及泛型简介
Collection接口 Collection 通用的常见方法 add()添加一个元素,可以指定脚标 addAll()将一个collection放入 clear()清除 remove()删除元素,返回 ...
随机推荐
- [SHELL进阶] (转)最牛B的 Linux Shell 命令 (三)
1. 更友好的显示当前挂载的文件系统 mount | column -t 这条命令适用于任何文件系统,column 用于把输出结果进行列表格式化操作,这里最主要的目的是让大家熟悉一下 columnt ...
- PHP date 格式化一个本地时间/日期
PHP date 格式化一个本地时间/日期 date (PHP 4, PHP 5) date — 格式化一个本地时间/日期 说明 string date ( string $format [, int ...
- PHP 魔术变量
PHP 魔术变量 PHP 向它运行的任何脚本提供了大量的预定义常量. 不过很多常量都是由不同的扩展库定义的,只有在加载了这些扩展库时才会出现,或者动态加载后,或者在编译时已经包括进去了. 有八个魔术常 ...
- 达内培训:php在线端口扫描器
达内培训:php在线端口扫描器 [来源] 达内 [编辑] 达内 [时间]2012-12-21 这个扫描器很简单.就是用了一个数组来定义端口的相关信息,原理就是用fsockopen函数连接,如 ...
- PHP文件操作 之读取目录信息
//定义一个函数 读取目录信息的函数 function dirInfo($dirName) { //判断目录是否存在 if (!file_exists($dirName)) { die('目录不存在! ...
- memcached使用详解
不错的文章 http://www.ttlsa.com/memcache/memcached-description/
- Nginx部署ThinkPHP项目的办法
thinkphp config配置: ', //URL模式 nginx rewrite配置: location / { if (!-e $request_filename) { rewrite ^(. ...
- Stationary point
https://en.wikipedia.org/wiki/Stationary_point https://zh.wikipedia.org/wiki/驻点
- access violation at address General protection fault
https://en.wikipedia.org/wiki/General_protection_fault In memory errors, the faulting program access ...
- phpcms v9 模板调用代码大全
另:每个栏目会对应当前所选模型的三个模板文件: 这些模板文件所在位置:phpcms/templates/default/content/ 目录下,如果想修改模板文件,只需要到此目录下找到对应的模板文 ...