String类:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
String是一个final类,代表不可变的字符序列。
字符串是常量,用双引号引起来表示。它们的值在创建之后不能更改。
String对象的字符内容是存储在一个字符数组value[]中的。
String的存储结构:
获取两个字符串中最大相同子串
class Test {
public static void main(String[] args) {
String str1 = "abcdefhijkbcdf", str2 = "abcdf";
HashMap<String, Integer> hashMap = new HashMap<>();
for (int i = 0; i < str2.length(); i++) {
for (int j = i, x = str2.length(); x > i; x--) {
if (str1.contains(str2.substring(i, x))) {
hashMap.put(str2.substring(i, x), str2.substring(i, x).length());
break;
}
}
}
Integer max = Collections.max(hashMap.values());
for (String s : hashMap.keySet()) {
if (hashMap.get(s) == max) {
System.out.println(s);
}
}
}
//方法2
public static List<String> getMaxSameSubString1(String str1, String str2) {
if (str1 != null && str2 != null) {
List<String> list = new ArrayList<String>();
String maxString = (str1.length() > str2.length()) ? str1 : str2;
String minString = (str1.length() > str2.length()) ? str2 : str1;
int len = minString.length();
for (int i = 0; i < len; i++) {
for (int x = 0, y = len - i; y <= len; x++, y++) {
String subString = minString.substring(x, y);
if (maxString.contains(subString)) {
list.add(subString);
}
}
if (list.size() != 0) {
break;
}
}
return list;
}
return null;
}
String:不可变字符序列
StringBuffer:可变字符序列、效率低、线程安全
StringBuilder:可变字符序列、效率高、线程不安全
JAVA比较器
Comparable:
实现Comparable接口,重写compareTo方法
class Goods implements Comparable {
private String name;
private double price;
//按照价格,比较商品的大小
@Override
public int compareTo(Object o) {
if(o instanceof Goods) {
Goods other = (Goods) o;
if (this.price > other.price) {
return 1;
} else if (this.price < other.price) {
return -1;
}
return 0;
}
throw new RuntimeException("输入的数据类型不一致");
}
//构造器、getter、setter、toString()方法略
}
定制比较器:Comparator<>():
class test2 {
public static void main(String[] args) {
Person aa = new Person("AA", 12);
Person bb = new Person("BB", 15);
Person cc = new Person("CC", 12);
ArrayList<Person> list = new ArrayList<>();
list.add(aa);
list.add(bb);
list.add(cc);
for (Person person : list) {
System.out.println(person.getAge()+person.getName());
}
list.sort(new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Person && o2 instanceof Person) {
Person a= (Person) o1;
Person b= (Person) o2;
if (a.getAge() != b.getAge()) {
return Integer.compare(a.getAge(), b.getAge());
} else {
return a.getName().compareTo(b.getName());
}
}
throw new RuntimeException("类型不对");
}
});
System.out.println("==========");
for (Person person : list) {
System.out.println(person.getAge()+person.getName());
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person() {
}
}
System类
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。
由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便的进行调用
native long currentTimeMillis():
void exit(int status):该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等
void gc():垃圾回收机制只回收JVM堆内存里的对象空间。
String getProperty(String key):
BigDecimal类
一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类。
时间类:
new SimpleDateFormat()
class TimeTest{
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
System.out.println(date);
Date format = sdf.parse("2020-08-12 12:30:55");
String format1 = sdf.format(format);
System.out.println(format1);
}
}
Calendar类 抽象类
class TimeTest{
public static void main(String[] args) throws ParseException {
Calendar calendar = Calendar.getInstance();
// 从一个 Calendar 对象中获取 Date 对象
Date date = calendar.getTime();
System.out.println(date);
calendar.set(Calendar.DAY_OF_MONTH, 9);
System.out.println("当前时间日设置为9号,时间是:" + calendar.getTime());
calendar.add(Calendar.HOUR, 2);
System.out.println("当前时间加2小时后,时间是:" + calendar.getTime());
calendar.add(Calendar.MONTH, -2);
System.out.println("当前日期减2个月后,时间是:" + calendar.getTime());
}
}
JDK8中新日期时间API
LocalDate、LocalTime、LocalDateTime 类是其中较重要的几个类,类似Calendar,但是它们的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间
class TimeTest{
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();//2020-08-15
LocalTime localTime = LocalTime.now();//23:44:10.772
LocalDateTime localDateTime = LocalDateTime.now();//2020-08-15T23:44:10.772
//自定义时间,无偏移,Calendar会有偏移
LocalDateTime dateTime = LocalDateTime.of(LocalDate.of(2020, 8, 8), LocalTime.of(8, 8, 8));
System.out.println(dateTime);//2020-08-08T08:08:08
int dayOfMonth = dateTime.getDayOfMonth();//8
int year = dateTime.getYear();//2020
Month month = dateTime.getMonth();//AUGUST
int monthValue = dateTime.getMonthValue();//8
LocalDateTime withHour = dateTime.withHour(18);//2020-08-08T18:08:08
LocalDateTime plusDays = dateTime.plusDays(5);//2020-08-13T08:08:08
LocalDateTime minusDays = dateTime.minusDays(5);//2020-08-03T08:08:08
}
}
Instant:
时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳。类似getTime();
class TimeTest{
public static void main(String[] args) {
Date date = new Date();//Sun Aug 16 00:06:08 CST 2020\
Instant instant = Instant.now();//返回UTC时间 2020-08-15T16:06:08.491Z 差8小时
OffsetDateTime atOffset = instant.atOffset(ZoneOffset.ofHours(8));//2020-08-15T00:06:08.432+08:00
long l = instant.toEpochMilli();//1597507901277
Instant instant1 = Instant.ofEpochMilli(1597507901277l);//2020-08-15T16:11:41.277Z
}
}
Duration,Period
计算时间日期间隔
class TimeTest{
public static void main(String[] args) {
LocalTime now = LocalTime.now();
LocalTime plusHours = now.plusHours(8);
Duration between1 = Duration.between(now, plusHours);//计算时间间隔
LocalDate localDate = LocalDate.now();
LocalDate plusDays = localDate.plusDays(8);
Period between = Period.between(localDate, plusDays);//计算日期间隔
}
}
DateTimeFormatter
格式化时间 类似SimpleDateFormat()
class TimeTest{
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = formatter.format(LocalDateTime.now());
System.out.println(format);//2020-08-16 00:33:09
TemporalAccessor parse = formatter.parse("2020-08-16 00:32:52");
System.out.println(parse);//{},ISO resolved to 2020-08-16T00:32:52
}
}
枚举类
使用 enum 定义的枚举类默认继承了 java.lang.Enum类,因此不能再继承其他类
枚举类的构造器只能使用 private 权限修饰符
枚举类的所有实例必须在枚举类中显式列出(, 分隔 ; 结尾)。列出的实例系统会自动添加 public static final 修饰
必须在枚举类的第一行声明枚举类对象
常用方法:
values()方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值。
alueOf(String str):可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的“名字”。如不是,会有运行时异常:IllegalArgumentException。
toString():返回当前枚举类对象常量的名称
class Test {
public static void main(String[] args) {
System.out.println(Person.lufei.getName());//路飞
Person lufei = Person.valueOf("lufei");
lufei.fight();//使用三挡
Person[] values = Person.values();
System.out.println(Arrays.asList(values));//[lufei, suolong]
String s = Person.suolong.toString();//suolong
}
}
enum Person {
lufei("路飞",18),
suolong("索隆",20);
private final String name;
private final Integer age;
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
private Person(String name ,Integer age) {
this.name=name;
this.age=age;
}
void fight(){
if (this.name==lufei.getName()){
System.out.println("使用三挡");
}else {
System.out.println("使用三刀流");
}
}
}
使用接口组织枚举
//另一个文件
public interface Fihjt {
enum Lufei implements Fihjt {
见闻色, 霸王色, 武装色, 流樱
}
enum Suolong implements Fihjt {
一刀流, 二刀流, 三刀流
}
}
class test {
public static void main(String[] args) {
for (Fihjt.Lufei val : Fihjt.Lufei.values()) {
System.out.println(val);
}
System.out.println(Fihjt.Suolong.三刀流);
}
}
#include <bits/stdc++.h>using namespace std;constexpr int INF = 1e9;vector<int> H; // h(x)void LoadTopo(vector<vector<int>> &topo, int nodeNum){ topo.resize(nodeNum + 1); int u, v, w; // while (cin >>
** Vue+element UI的动态表单的校验(根据条件动态切换校验格式)**1.整个表单是可新增的,所以要遍历生成;2.因为input 是动态生成的,所以检验规则也需要动态生成实现elementui的form表单实现校验的时候要给当前el-form-item加上prop属性,因为我们是遍历生成的表单,那我们的写法就要写成:重点在prop属性<div v-for="(item,index) in formData.lists"> <el-form-item :
利用pcl中的kdtree可以做到搜索关键点某半径邻域内的区域.主要步骤1.读入点云数据2.设置kdtree3.设置关键点和邻域半径4.执行搜索函数附上代码#include <iostream>#include <pcl/point_types.h>#include <pcl/io/pcd_io.h>#include <pcl/io/ply_io.h>#include <pcl/kdtree/kdtree_flann.h>#
学习用GestureDetector来检测屏幕滑动事件的时候,一开始对onTouchEvent复写的时候写成了下面代码中(1)的样子 @Override public boolean onTouchEvent(MotionEvent event) { return new GestureDetector(new MyOnTouchListener()).onTouch
目的 通过当前文档的内容,可以将springboot类的微服务项目基于Docker自动构建出成品,可以省去了大量的项目部署时间以及项目依赖环境的部署时间。 阅读本章内容,请确保你已掌握Docker到基本使用,如未满足该要求,请到Docker操作指南中进行学习。处理流程提交项目业务代码和构建代码到SVNJenkins 构建任务部署包上传FTP测试人员从FTP下载部署包,使用脚本一键安装部署包
从三月初到四月末,从完全懵逼到熟悉各种套路,从耿直实诚到睁眼说瞎话到最后各种吹水,看了不少面经,是时候也做一些细微的工作了。 先来说说面试体会吧!其实每年春招秋招,总有这么一类人,简称offer收割机,这类人一般出身名校,要不比赛经验丰富(CTF赛棍),要不工程能力极强(github一天的star比我一个月的还多),要不精通算法(ACM各种大奖),这类人面试一般面一家就拿一家offe...
1.对发送的数据进行 gzip压缩 、Base64.encode编码、URLEncoder.encode编码,最后再进行http传输数据 源数据 --> gzip压缩 --> Base64.encode编码 --> URLEncoder.encode编码 --> http传输数据2.对http接收到的数据进行 URLDecoder.decode解码、B...
首先,使用svn时,对于svn的协议要明确:你到底使用哪个协议来完成访问svn库的操作?svn协议有以下两种:http(Apache); svn(svnserve)在局域网内部使用svn,一般用svnserve访问svn,如果需要通过网络访问用http比较适合。但是!!!如果自己在一台服务器上建立svn,那么在同一台服务器上不管哪个用户,只要用svn co file:///path/to/my/s
为什么80%的码农都做不了架构师?>>> ...
问题新开的Windows Server 虚拟机部分出现远程桌面连接不上的问题。报错不一样,有无法连接远程桌面,请联系管理员的,也有出现内部错误的。暂时的解决办法因为是有些虚拟机出现这种问题,但是有些又没有出现,只能通过VNC之类的方式登陆到虚拟机里面,然后重启Remote Desktop Services:打开service:把这个服务重启:暂时就可以解决这个问题。可能还会有防火墙之类的问题,这个就要在防火墙配置里面打开3389(远程桌面连接端口)的放行规则,但是一般这个规则都是允许域
默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量。此时不能在命令行直接使用python命令。1.首先需要在系统中注册python环境变量:假设python的安装路径为c:\python2.6,则修改我的电脑->属性->高级->环境变量->系统变量中的PATH为:(为了在命令行模式下运行Python命令,需要将python.exe所在的目录附加到PATH这个环境变量中。)`PATH=PATH;c:\python26`上述环境变量设置成功之后..
查一下符点数存储标准IEEE754就知道了!符点数是采用二进制科学计数法来进行存储的,因此,绝大多数的数在计算机中是不能 精确 表示的。如果两个数a b都是常数赋值,这样比较,应该不会有问题,如:double a=5.3,b=5.3;if ( a == b ){printf("a=b\n");}这时会输出a=b如果a或b是经过运算后得到的值,