冒泡排序:
原理:比较两个相邻的元素,将值大的元素交换至右端。
思路:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。重复第一趟步骤,直至全部排序完成。
第一趟比较完成后,最后一个数一定是数组中最大的一个数,所以第二趟比较的时候最后一个数不参与比较;
第二趟比较完成后,倒数第二个数也一定是数组中第二大的数,所以第三趟比较的时候最后两个数不参与比较;
依次类推,每一趟比较次数-1;
==============================================================================================
常用类
- String 不可变长的字符序列 “abc” Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现
-
其内部是由字符串组表示 private final char value[];
- String : 不可变长字符串
- StringBuilder:可变长字符串,线程不安全的
- StringBuffer:可变长字符串,线程安全的
- String:构造器
public class StringDemo01 extends Object{
public static void main(String[] args) throws UnsupportedEncodingException {
String str=“abc”; //1个 字符串常量池中 “abc”
String str4=“abc”; //1个 字符串常量池中 “abc”
//String(String original) 初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列
// String str2=new String(“cde”); //2个 new ->String,堆中 字符串常量池中"cde"
String str2=new String(“abc”); //1个 new ->String,堆中
String str3=new String(“abc”);
System.out.println(str); //abc
System.out.println(str2); //abc
System.out.println(str2str); //false
System.out.println(str2.equals(str)); //true
System.out.println(str2str3); //false
System.out.println(str==str4); //true
//String() 初始化一个新创建的 String 对象,使其表示一个空字符序列
String s1=new String();
//String(char[] value) 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
String s2=new String(new char[]{'s','h','s','x','t'});
System.out.println(s2);
//String(char[] value, int offset, int count) 分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
String s3=new String(new char[]{'s','h','s','x','t'},2,3);
System.out.println(s3);
//String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
String string="因为";
byte[] arr=string.getBytes();
System.out.println(Arrays.toString(arr));
String s4=new String(new byte[]{-27, -101, -96, -28, -72, -70});
System.out.println(s4);
//String(byte[] bytes, Charset charset)
String s5=new String(new byte[]{-27, -101, -96, -28, -72, -70},"GBK");
System.out.println(s5);
//String(byte[] bytes, int offset, int length)
String s6=new String(new byte[]{-27, -101, -96, -28, -72, -70},3,3);
System.out.println(s6);
}
String的方法:
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class StringDemo02 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str=“shsxtgood”;
String str2=“Shsxtgood”;
System.out.println(str);
//char charAt(int index) 返回指定索引处的 char 值。 ***
System.out.println(“charAt():”+str.charAt(2));
//int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引 ***
System.out.println(“indexOf():”+str.indexOf(“s”));
//int indexOf(String str, int fromIndex)
System.out.println(“indexOf(),指定起始索引:”+str.indexOf(“s”,1));
//int lastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引。
System.out.println(“lastIndexOf():”+str.lastIndexOf(“s”));
// int codePointAt(int index) 返回指定索引处的字符(Unicode 代码点)。
System.out.println("charAt():"+str.codePointAt(2)); //115
// int compareTo(String anotherString) 按字典顺序比较两个字符串。
//相同返回0,不同 使用当前字符串与参数字符串同一个位置的字符进行减法
System.out.println("compareTo()"+str2.compareTo(str)); //0
//int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,不考虑大小写。
System.out.println("compareToIgnoreCase()"+str2.compareToIgnoreCase(str));
// String concat(String str) 将指定字符串连接到此字符串的结尾。 返回新串
System.out.println("concat()"+str.concat(str2)); //shsxtgoodShsxtgood
//boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。
System.out.println("contains()"+str.contains("sxt")); //true
//static String copyValueOf(char[] data) 字符数组转为字符串
//static String copyValueOf(char[] data, int offset, int count)
//boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始。
//boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。
System.out.println("endsWith()"+str.endsWith("d")); //true
/*
* byte[] getBytes() ***
使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
byte[] getBytes(Charset charset)
*/
byte[] arr="你好".getBytes(); //utf-8
byte[] arr2="你好".getBytes("gbk"); //gbk
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
//void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)将字符从此字符串复制到目标字符数组。
char[] charArr=new char[6];
"你好哈哈哈".getChars(1, 4, charArr, 3);
System.out.println(charArr.length);
//length() *****
System.out.println(str.length());
// String replace(char oldChar, char newChar) 新串替换老串 ***
System.out.println("replace()"+str.replace("s","S"));
//tring[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。 ****
String str3="name=zhangsan";
String[] s=str3.split("=");
System.out.println(Arrays.toString(s));
/*
* String substring(int beginIndex) *****
返回一个新的字符串,它是此字符串的一个子字符串。
String substring(int beginIndex, int endIndex) endIndex不包含
返回一个新字符串,它是此字符串的一个子字符串。
*/
System.out.println("substring()"+str.substring(3));
System.out.println("substring()"+str.substring(2,5));
// char[] toCharArray() 将此字符串转换为一个新的字符数组。 ***
char[] ch=str.toCharArray();
System.out.println(Arrays.toString(ch));
//String toLowerCase() 转小写
//String toUpperCase() 转大写
System.out.println("toLowerCase()"+str2.toLowerCase());
System.out.println("toUpperCase()"+str2.toUpperCase());
//String trim()返回字符串的副本,忽略前导空白和尾部空白。
System.out.println(" yinwei ".trim());
//valueOf() 参数转为字符串
double d=123.123;
System.out.println(String.valueOf(d).length());
}
}
- StringBuilder:可变长字符串,线程不安全的,效率较高
- StringBuffer:可变长字符串,线程安全的,效率较低
public class StringDemo03 {
public static void main(String[] args) {
//StringBuilder() 构造一个其中不带字符的字符串生成器,初始容量为 16 个字符。
StringBuilder sb=new StringBuilder();
System.out.println(sb);
System.out.println(sb.capacity()); //16
System.out.println(sb.length());
//StringBuilder(String str) 构造一个字符串生成器,并初始化为指定的字符串内容。
StringBuilder sb1=new StringBuilder("abc");
System.out.println(sb1);
System.out.println(sb1.capacity());
System.out.println(sb1.length());
//StringBuilder(int capacity) 构造一个其中不带字符的字符串生成器,初始容量由 capacity 参数指定。
StringBuilder sb2=new StringBuilder(15);
System.out.println(sb2);
System.out.println(sb2.capacity());
System.out.println(sb2.length());
//StringBuilder append(boolean b) 拼接参数值
/*
* append扩容: 如果原容量放不下进行扩容, 原容量size-->size*2+2,如果扩容后的大小还是放不下,直接以内容的长度进行扩容
*/
StringBuilder s=sb.append("12345678901234567");
sb.append(123.123);
System.out.println(sb);
System.out.println(s==sb);
System.out.println(sb.capacity()); //16
System.out.println(sb.length()); //4
//StringBuilder delete(int start, int end) 不包含end
sb.delete(2, 4);
System.out.println(sb);
//StringBuilder insert(int offset, boolean b)
sb.insert(3,true);
System.out.println(sb);
//StringBuilder reverse()
sb.reverse();
System.out.println(sb);
//String str="abc"==>倒叙打印
}
}
==============================================================================================
基本数据类型的包装类型
- byte ----- Byte
- short ----- Short
- int ----- Integer
- long ----- Long
- float ----- Float
- double ----- Double
- char ----- Character
- boolean ----- Boolean
- 自动装箱: 从基本数据类型->包装类型
- 自动拆箱: 从包装类型->基本数据类型
- Integer 类在对象中包装了一个基本类型 int 的值
public class DataTypeDemo01 extends Object{
public static void main(String[] args) {
int i=101; //int类型
Integer i2=i; //自动装箱 Integer.valueof(i2)
int i3=i2; //自动拆箱 i2.intValue()
haha(1.1,2.2);
int int1=127;
int int2=127;
System.out.println(int1==int2); //true
//缓冲区对象的表示范围: [-128,127] ==>valueOf()
Integer int3=127;
Integer int4=127;
System.out.println(int3==int4); //true
Integer int5=new Integer(127);
Integer int6=new Integer(127);
System.out.println(int5==int6); //false
Integer int7=128;
Integer int8=128;
System.out.println(int7==int8); //false
//自动 拆箱比较
System.out.println(int1==int3); //true
System.out.println(int1==int5); //true
System.out.println(int3==int5); //false
/*
* 1.int 和 Integer(无论是否new)比较:发生自动拆箱,如果值相同就相同
* 2.如果两个Integer比较,如果有new,就不相同
* 3.如果都没有new,要看是否再缓冲区对象的范围之内,在相同,不在不相同
*/
Double d1=1.0;
Double d2=1.0;
System.out.println(d1==d2);
/*
* public static Double valueOf(double d) {
return new Double(d);
}
*/
/*
* static int parseInt(String s)
将字符串参数作为有符号的十进制整数进行解析。
static int parseInt(String s, int radix) radix是指定使用什么进制解析参数s
*/
// System.out.println(Integer.parseInt(“abc123”));//NumberFormatException
System.out.println(Integer.parseInt(“10”));// 10
System.out.println(Integer.parseInt(“10”,2));//2
String str="a"+"b"+"c"+"d"; //"abcd" 1个 编译器会自动优化
str="haha"+new String("abc");
}
public static double haha(Double d1,Double d2){
return d1+d2;
}
}
==============================================================================================
Date 日期类
- 构造器:
-
Date() 根据当前时间创建日期对象(本地)
-
Date(long time) 根据long类型的毫秒数构建指定时间的日期对象
public class DateDemo {
public static void main(String[] args) {
Date date=new Date();
Date date4=new Date();
System.out.println(date);
System.out.println(date.getTime()); //获取从1970到当前时间的毫秒数
Date date2=new Date(1560151689048L);
System.out.println(date2);
/*
* boolean after(Date when)
测试此日期是否在指定日期之后。
boolean before(Date when)
测试此日期是否在指定日期之前。
Object clone()
返回此对象的副本。
int compareTo(Date anotherDate)
比较两个日期的顺序。
boolean equals(Object obj)
比较两个日期的相等性。
*/
System.out.println(date.after(date2)); //ture
System.out.println(date.before(date2)); //false
System.out.println(date.compareTo(date2));
System.out.println(date.compareTo(new Date()));
System.out.println(date.compareTo(date4));
/*
* void setTime(long time)
设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
*/
date.setTime(1560151689048L);
date.setTime(0);
System.out.println(date); //Thu Jan 01 08:00:00 CST 1970
System.out.println(date4.toString());
}
}
-
y : 年
-
M : 月
-
d: 日期
-
H : 24小时
-
h : 12小时
-
m : 分
-
s : 秒
-
S : 毫秒
-
设置转换器格式:SimpleDateFormat对象
-
SimpleDateFormat() 默认的转换格式 用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。 SimpleDateFormat(String pattern) 指定转换格式
-
format(Date) 日期对象准为字符串
-
parse(String) 字符串准为日期对象
*/
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
//默认转换器
SimpleDateFormat simple=new SimpleDateFormat();
System.out.println(simple.format(new Date())); //19-6-10 下午3:40//指定格式转换器 SimpleDateFormat simple2=new SimpleDateFormat("yyyy/MM/dd E hh:mm:ss SSS"); System.out.println(simple2.format(new Date())); String str="2018年3月12日"; SimpleDateFormat simple3=new SimpleDateFormat("yyyy年MM月dd日"); System.out.println(simple3.parse(str));
}
}
==============================================================================================
import java.io.File;
/*
File文件类
-
文件和目录路径名的抽象表示形式。
-
构造器:
-
方法
*/
public class FileDemo {
public static void main(String[] args) {
//File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File file1=new File(“haha.txt”);
// File file2=new File(“D:\haha.txt”);
// File file2=new File(“D://haha.txt”);
File file2=new File(“D:/test.txt”);
// File file2=new File(“D:/AAA”);
File parent=new File(“D:/”);
//File(File parent, String child)
File file3=new File(parent,“test.txt”);//File(String parent, String child) File file4=new File("D:/","test.txt"); System.out.println(file1); System.out.println(file2); System.out.println(file3); System.out.println(file2.equals(file3)); System.out.println(file4);
}
}