枚举
public enum A{
X,Y,Z;
//……;
}
第一行都是常量
里面构造器都是私有
最终类,不可以被继承
枚举的基本思想:可以做信息标志和分类,用来解决特定常量的分配使用
零碎知识点
一个java类只能有一个public类,但可以定义多个非public类
deleteOnExit()先声明,等程序运行结束, JVM终止时才真正调用删除操作
void write() / int read()
正则表达式里[]里只能匹配一个字符matches
Java 实例 - 将文件内容复制到另一个文件
public static void main(String[] args) throws Exception{
BufferedWriter out1 = new BufferedWriter(new FileWriter("srcfile"));
out1.write("string to be copied");
out1.close();
InputStream in = new FileInputStream(new File("srcfile"));
OutputStream out = new FileOutputStream(new File("destnfile"));
byte[] buf = new byte[1024];
int len;
while((len = in.read(buf))> 0){
out.write(buf,0,len);
}
in.close();
out.close();
BufferedReader in1 = new BufferedReader(new FileReader("destnfile"));
String str;
while((str = in1.readLine()) != null){
System.out.println(str);
}
in1.close();
xml
只有一个根标签
CD创造特殊区域,可以写特殊符号比如小于号
foreach
for(元素类型t 元素变量x : 遍历对象obj){
引用了x的java语句;
}
如果是集合p,则直接p.for
囚犯案例
分析
1.确立对象,把囚犯的相关信息封装成对象People
public class Peoples {
private int num;
private int location;
public Peoples() {
}
public Peoples(int num, int location) {
this.num = num;
this.location = location;
}
2.建立集合,ArrayList运用add方法,将People的信息add
public static ArrayList<Peoples> p = new ArrayList<>();
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 100; i++) {
int num = r.nextInt(200)+1;
if(isOk(num)){
Peoples peoples = new Peoples(num,i+1);
p.add(peoples);
}else{
i--;
}
}
System.out.println(p);
3.进行删减
while(p.size()>1){
List<Peoples> tempeople = new ArrayList<>();
for (int i = 1; i < p.size(); i+=2) {
Peoples peoples = p.get(i);
tempeople.add(peoples);
}
p = (ArrayList<Peoples>) tempeople;
}
Peoples luckman = p.get(0);
System.out.println(luckman);
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/a381dc9a8d854c018afc11a459f66514.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/4de8e5e2e88140cf9dd9756211c928e6.png)