1.数字格式化
就是一个按照指定格式输出数字的方式,同志们
下面是把97664变成¥97,664.00这样格式的数字的代码
public class Demo {
public static void main(String[] args) throws ParseException {
// \u00A4 表示货币字符(¥); #表示数字,0时不显示 0表示数字,0时显示
String decimalPattern="\u00A4#,##0.00";
//构造DecimalFormat对象
DecimalFormat df=new DecimalFormat(decimalPattern);
System.out.println(df.format(97664)); //输出¥97,664.00
System.out.println(df.format(326.50)); //输出¥326.50
System.out.println(df.format(4348.11)); //输出¥4,348.11
System.out.println(df.parse("¥97,664.00")); //输出97664
}
}
获取更多资讯请登录http://www.jianshu.com/p/b3699d73142e
2.正则表达式
就是判断字符串是否符合条件的
下面是实例,同志们
public class PatternDemo {
public static void main(String[] args) {
// 定义字符串格式
Pattern pattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]{4,15}$");
// new Scanner(new File("D:/abc.txt"))
// new Scanner(new File("D//abc.txt"))
// "\w"这俩在一起的时候代表单词字符
// "\\w"才可以,第一个"\"用于转义,第二个"\"是个普通的"\"
// Pattern pattern = Pattern.compile("^[a-zA-Z]\\w{4,15}$");
//输入要判断的字符串
Matcher matcher = pattern.matcher("z2ang43san");
boolean b = matcher.matches();
System.out.println(b); //输出true
}
}
public class Pattern1 {
public static void main(String[] args) {
//或者使用这两种方法
//判断是不是QQ号格式的
//第一种方法
//boolean matches = Pattern.matches("[1-9][0-9]{4,}", "116336844");
//System.out.println(matches);
//第二种方法
String qq = "116336844";
boolean matches = qq.matches("[1-9][0-9]{4,}");
System.out.println(matches);
}
}
3.集合
一种数据结构,
用数组存储数据增删查改太麻烦,长度还不能变
我们用集合存储操作起来更方便些
下面是 集合的创建 and 集合中的方法
(1)
public class CollectionDemo {
public static void main(String[] args) {
// 数组实现
Collection collection=new ArrayList();
// 1. add() 将给定的对象添加到该集合中
collection.add(123);
collection.add("123");
collection.add(new Date());
// 2. clear() 清空集合中的所有元素
// collection.clear();
// System.out.println(collection);
// 3. remove() 把给定的元素从集合中删除
//是通过equals()方法进行判断的
collection.add(new Student("1001","李华旭","男","量子物理"));
collection.add(new Student("1001","李华旭","男","量子物理"));
collection.remove(new Student("1001","李华旭","男","量子物理"));
collection.remove("123");
// 4. contains() 判断集合中是否包含给定的对象
//也是用equals()方法判断对象在不在
//System.out.println(collection.contains(new Student("1001","李华旭","男","量子物理")));
// 5. isEmpty() 判断该集合是否为空
// boolean empty = collection.isEmpty();
// System.out.println(empty);
// 6.toArray() 把集合中的元素存储到数组中
// Object[] objects = collection.toArray();
}
}
(2)
public class CollectionDemo2 {
public static void main(String[] args) {
//Set实现
Collection collection=new HashSet();
collection.add(123);
collection.add(123);
collection.add(123);
collection.add(123);
collection.add("123");
collection.add("123");
collection.add("123");
collection.add("123");
//最后输出的是这个: [123, 123]
//也是通过equals()方法进行判断
//要是数值相同就不添加了
//collection.add(new Student("1001","杨志冰","女","统计学"));
System.out.println(collection);
}
}
(3)
public class CollectionDemo3 {
public static void main(String[] args) {
//这样写一上来就报黄了
//Collection students=new ArrayList();
// <> 这里面写添加进去的对象时什么类型的
//这样就不报黄了
Collection<Student> students=new ArrayList<>();
//如果不加<> 这样取出来的数据都是object类型的
//如果想要调用Student类的方法,就要做强转,
// 但一遍遍强转太麻烦了
students.add(new Student("1001","赵振宁","男","水产养殖"));
students.add(new Student("1001","赵振宁","男","水产养殖"));
students.add(new Student("1001","赵振宁","男","水产养殖"));
students.add(new Student("1001","赵振宁","男","水产养殖"));
//加上<> 就可为加进去的东西指定类型,不用一遍遍强转了
System.out.println(students);
}
}
4.Iterator接口 (迭代器)
就是用来得到集合里所有元素的
迭代的意思就是:
遍历集合,判断里面有没有元素,如果有就取出,
然后再判断,再取出,直到取完为止
public class CollectionDemo4 {
public static void main(String[] args) {
Collection<Student> students=new ArrayList<>();
students.add(new Student("1001","縢昱旸","男","计算机科学"));
students.add(new Student("1002","縢昱旸","男","计算机科学"));
students.add(new Student("1003","縢昱旸","男","计算机科学"));
students.add(new Student("1004","縢昱旸","男","计算机科学"));
students.add(new Student("1005","縢昱旸","男","计算机科学"));
students.add(new Student("1006","縢昱旸","男","计算机科学"));
Iterator<Student> iterator = students.iterator();
//iterator提供了两个方法:
// 1. hasNext() 用于判断后面还有没有元素
while (iterator.hasNext()){
// 2. next() 用于读取后面的元素,得到一个对象
Student student = iterator.next();
//遍历的时候是不可以加元素的
//students.add(new Student("1001","縢昱旸","男","计算机科学"));
System.out.println(student);
}
}
}
这样就可以输出集合里面的所有对象的信息了(’∇’)シ┳━┳
THE END