惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
I
Intezer
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
小众软件
小众软件
T
Threatpost
B
Blog
美团技术团队
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Cyberwarzone
Cyberwarzone
雷峰网
雷峰网
The GitHub Blog
The GitHub Blog
T
Tenable Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
博客园 - 三生石上(FineUI控件)
L
LINUX DO - 热门话题
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
S
Security Affairs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
GbyAI
GbyAI
N
News and Events Feed by Topic
Cloudbric
Cloudbric
WordPress大学
WordPress大学
量子位
W
WeLiveSecurity
H
Hacker News: Front Page
Project Zero
Project Zero
S
Security @ Cisco Blogs
Security Latest
Security Latest
Hugging Face - Blog
Hugging Face - Blog
Forbes - Security
Forbes - Security
C
Cybersecurity and Infrastructure Security Agency CISA
人人都是产品经理
人人都是产品经理
U
Unit 42
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
2016-04-19 · via 轶哥博客

  三个简单的JAVA练习题。

  1. 建立一个元素类型java.util.Date的线性表,并在表中加入3个日期对象。
  2. 定义一个代表人员的类Person,含有三个数据成员(姓名,性别,年龄)。用TreeSet<Person>存储三个对象。
  3. 产生0-99的随机数100个,去掉重复的,还有多少个相互不同的数?

  注意:因为虚拟机版本问题,在Win和Mac平台中有一小点不同。虽然Java是跨平台的,但不同版本的Java虚拟机对语法的要求还是有略微区别,理论上直接编译都是可以的,但强迫症的轶哥见不得IDE报错。

Win平台Main.java:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {

    public static void main(String[] args) throws ParseException {
        //第一题
        ArrayList&lt;Date&gt; list_one = new ArrayList&lt;&gt;();
        list_one.add(new Date());//添加当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = "2016-4-19";
        Date date = sdf.parse(strDate);//按照格式添加指定字符串日期
        list_one.add(date);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        list_one.add(sdf2.parse("2016-4-19 14:09:32"));//添加包含时分秒的日期
        System.out.println(list_one);

        //第二题
        TreeSet&lt;Person&gt; t = new TreeSet&lt;&gt;();
        t.add(new Person("张三", "男", 15));
        t.add(new Person("李四", "女", 35));
        t.add(new Person("王五", "男", 5));
        System.out.println(t);

        //第三题
        Random r = new Random();
        List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
        int i;
        while (list.size() &lt;= 100) {
            i = r.nextInt(99);//生成0-99的随机数
            //if(!list.contains(i)){ 如果这样判断,可以在添加的时候就去除重复的数
            list.add(i);
            //}
        }
        List&lt;Integer&gt; listTemp = new ArrayList&lt;&gt;();
        Iterator&lt;Integer&gt; it = list.iterator();
        while (it.hasNext()) {
            int a = it.next();
            if (listTemp.contains(a)) {
                it.remove();
            } else {
                listTemp.add(a);
            }
        }
        System.out.println("去掉重复的还剩下:" + list.size() + "个");
    }
}

Mac平台Main.java:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {

    public static void main(String[] args) throws ParseException {
        //第一题
        ArrayList&lt;Date&gt; list_one = new ArrayList&lt;Date&gt;();
        list_one.add(new Date());//添加当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = "2016-4-19";
        Date date = sdf.parse(strDate);//按照格式添加指定字符串日期
        list_one.add(date);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        list_one.add(sdf2.parse("2016-4-19 14:09:32"));//添加包含时分秒的日期
        System.out.println(list_one);

        //第二题
        TreeSet&lt;Person&gt; t = new TreeSet&lt;Person&gt;();
        t.add(new Person("张三", "男", 15));
        t.add(new Person("李四", "女", 35));
        t.add(new Person("王五", "男", 5));
        System.out.println(t);

        //第三题
        Random r = new Random();
        List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
        int i;
        while (list.size() &lt;= 100) {
            i = r.nextInt(99);//生成0-99的随机数
            //if(!list.contains(i)){ 如果这样判断,可以在添加的时候就去除重复的数
            list.add(i);
            //}
        }
        List&lt;Integer&gt; listTemp = new ArrayList&lt;Integer&gt;();
        Iterator&lt;Integer&gt; it = list.iterator();
        while (it.hasNext()) {
            int a = it.next();
            if (listTemp.contains(a)) {
                it.remove();
            } else {
                listTemp.add(a);
            }
        }
        System.out.println("去掉重复的还剩下:" + list.size() + "个");
    }
}

不分平台 Person.java :

class Person implements Comparable&lt;Person&gt;{
    private String name;
    private String sex;
    private int age;

    Person(String name, String sex, int age){
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String toString(){
        return this.name + " " + this.sex + " " + this.age;
    }

    @Override
    public int compareTo(Person o) {
        return 1;
    }
}

执行结果截图:

JAVA代码