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()删除元素,返回 ...
随机推荐
- eclipse常用窗口和功能总结
1.左上角File按钮功能:最重要的导入import第三方项目,maven,git,general等. File->import File->properties按钮,显示项目的存储位置, ...
- linux 常用的基本命令
$ ls # 查看文件列表 $ ls dir_name | more : 分页查看文件列表 $ ll -h dir_name # 以 KB.MB.GB格式查看文件大小 $ ll -Sh # --so ...
- SQL查询(一)
查询基础练习 练习一: <学生信息表>:students(学号,姓名,性别,出生年,省份,入学年份,班级) <学生选修信息表>:stucourses(学号,课程号,课程名称,分 ...
- wflag
http://stackoverflow.com/questions/41312622/how-to-echo-an-alert-in-php-a-string-some-confusion-with ...
- 二进制流 最后一段数据是最后一次读取的byte数组没填满造成的
while(in.read(temp)!=-1){ out.write(temp); } 改成: int len; while((len=in.read(temp))!=-1){out.write(t ...
- 【转】UGUI实现unity摇杆
http://blog.csdn.net/onafioo/article/details/46403801 http://www.winig.cc/archives/348 好久没有写文章了,最近在做 ...
- your project contains error(s),please fix them before running your application.错误总结
Android开发中的问题总是多种多样,今天我来总结一个浪费了我一个晚上的错误T-T:your project contains error(s),please fix them b ...
- EF的表连接方法Include()
在EF中表连接常用的有Join()和Include(),两者都可以实现两张表的连接,但又有所不同. 例如有个唱片表Album(AlbumId,Name,CreateDate,GenreId),表中含外 ...
- 【Android测试】【第四节】LogCat——认识和使用
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4682321.html 什么是LogCat LogCat可以 ...
- git warning解决方案
1.warning: LF will be replaced by CRLF in xxxxx. 设置: git config core.autocrlf false