Java学习——day 14

主要内容

  • 同步容器与只读控制
  • IEDA中引入外部jar包

笔记详情

1. 同步容器与只读控制

我们知道Java中的ArrayList、HashMap、LinkSet等容器都是线程不安全的,都是非同步的。如果我们在多线程编程过程中,需要将其设置为同步,可以用 Collections类的synchronizedMap、synchronizedSet、synchronizedList方法使HashMap具有同步的能力,或者使用ConcurrentHashMap。下面给出一个实例:

/**
 * 将非同步的List转成同步的List
 * Set、Map接口的转换方式和List的转换方式如出一辙
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo01 {

    public static void main(String[] args) {

        List<String> stringList1 = new ArrayList<String>();
        stringList1.add("Tom");
        stringList1.add("Jack");
        stringList1.add("Jane");
        
        // 同步的List
        List<String> stringList2 = Collections.synchronizedList(stringList1);
    }
}

只读控制,即对Java中的容器进行设置,将容器设置为只读的,一旦设置之后,容器中的元素就不能改变。只读控制有三类:将容器设置为空的、只有一个元素、元素不可改变的。可以通过Collections类提供的方法来实现:emptyXXX(XXX可以为List、Set、Map)、singletonXXX(XXX可以为空、List、Map,为空时表示Set)、unmodifiableXXX(XXX可以为List、Set、Map)。下面给出一个实例:

/**
 * 这里举出List实例,Set、Map与此同理
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo02 {

    public static void main(String[] args) {
        /*****************************定义一个空的List*****************************/
        List<String> stringList1 = Collections.emptyList();
        
        
        /*****************************定义一个只有一个元素的容器*****************************/
        List<String> stringList2 = Collections.singletonList("feng");
        // stringList2.add("222");  // 容器中只能有一个元素,不能再进行添加


        /*****************************定义一个不可变容器*****************************/
        List<String> stringList3 = new ArrayList<String>();

        stringList3.add("Tom");
        stringList3.add("Jack");
        stringList3.add("Jane");

        List<String> stringList4 = Collections.unmodifiableList(stringList3);
        // stringList4.add("feng");  容器是不可变的,不能再进行添加元素
    }
}
2. IEDA中引入外部jar包

在我们的项目中,可能会使用到一些成熟的框架,需要引入外部的一些jar包,在这里就总结一下,在IDEA中,如果引入外部jar包。

首先我们将下载的jar包放在合适的位置,我们需要通过该位置加载jar包。然后回到IEDA,点击【File】,选择【Project Structure】。

然后按照下图,依次点击【Modules】、【Depedencies】、【+】

点击【+】之后,选择第一个选项【JARs or directories】,然后找到下载的jar包,点击【OK】。然后再进行点击【OK】即可完成。

现在再回到项目中,我们就可以看到加载完成的外部jar包,这个时候就可以正常使用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现的代码: ```java import java.util.Scanner; public class MotherDay { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入年份:"); int year = sc.nextInt(); sc.close(); int month = 5; // 母亲节固定在5月 int day = getMotherDay(year, month); System.out.println(year + "年的母亲节是" + month + "月" + day + "日"); // 在月历中标注母亲节 printMonth(year, month, day); } // 计算母亲节的日期 public static int getMotherDay(int year, int month) { int day = 1; // 从1号开始查找 int week = getWeekDay(year, month, day); while (week != 0) { // 找到第一个星期日 day++; week = getWeekDay(year, month, day); } return day + 7; // 第二个星期日就是母亲节 } // 计算某一天是星期几 public static int getWeekDay(int year, int month, int day) { if (month == 1 || month == 2) { year--; month += 12; } int c = year / 100; int y = year % 100; int week = (c / 4 - 2 * c + y + y / 4 + 13 * (month + 1) / 5 + day - 1) % 7; if (week < 0) { week += 7; } return week; } // 在月历中标注母亲节 public static void printMonth(int year, int month, int day) { String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 判断闰年 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { daysInMonth[1] = 29; } // 打印月历 System.out.println("\n " + monthName[month - 1] + " " + year); System.out.println("------------------------------"); System.out.println(" Sun Mon Tue Wed Thu Fri Sat"); int weekDay = getWeekDay(year, month, 1); for (int i = 0; i < weekDay; i++) { System.out.print(" "); } for (int i = 1; i <= daysInMonth[month - 1]; i++) { if (i == day) { System.out.printf("|%2d|", i); // 标注母亲节 } else { System.out.printf("%4d", i); } weekDay = (weekDay + 1) % 7; if (weekDay == 0) { System.out.println(); } } System.out.println(); } } ``` 运行结果如下: ``` 请输入年份:2022 2022年的母亲节是5月8日 May 2022 ------------------------------ Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 | 8|| 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ``` 可以看到,在月历中标注了母亲节的日期(用竖线括起来的数字)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值