- 刷題真的很爽!
- 刷完題必定要把值得記錄的題記錄下來,方便複習,也能夠積累成就感,便於堅持!
/root/bootstrap/result/備用1/Exam-04/extra/itcast.txt文件中存儲了多行數字, 讀取每行的數字,將結果打印到控制檯。 將文件中讀取出的數字進行去重操做,將結果打印到控制檯。 將去重後的數字按照由小到大的方式進行排列,並將排列後的結果打印到控制檯。 將排序後的結果按行存儲,按行存儲,按行存儲(重要事情說三遍,須要換行)到/root/bootstrap/result/備用1/Exam-04/extra/itcast.avi文件中。
邏輯在Demo類的main函數中實現; 例如/root/bootstrap/result/備用1/Exam-04/extra/itcast.txt中按行存儲了 9,2,5,2,6,5,4,9 打印的結果: 第一行--> 初始: 9 2 5 2 6 5 4 9 第二行--> 去重: 9 2 5 6 4 第三行--> 排序: 2 4 5 6 9
package com.heima_IO; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.TreeSet; public class Demo { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\Dell xps\\Desktop\\itcast.txt"))); TreeSet<Integer> ts = new TreeSet<>(); String line; String startStr = "初始:"; while((line = br.readLine()) != null) { ts.add(Integer.parseInt(line)); startStr += line + " "; } System.out.println(startStr.trim()); String distinctStr = "去重:"; String sortStr = "排序:"; BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C:\\Users\\Dell xps\\Desktop\\itcast_copy.txt"))); for(int i : ts) { distinctStr += i + " "; sortStr += i + " "; bw.write("" + i); bw.newLine(); } System.out.println(distinctStr.trim()); System.out.println(sortStr.trim()); br.close(); bw.close(); } }
private static void demo01() throws IOException { //讀取文件 BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\Dell xps\\Desktop\\itcast.txt"))); BufferedWriter bw = new BufferedWriter(new FileWriter(new File("C:\\Users\\Dell xps\\Desktop\\itcast_copy.txt"))); //該容器用於字符串去重 HashSet<Character> hs = new HashSet<>(); //初始字符串 String startStr = "初始:"; String line; //按行取出字符串 while((line = br.readLine()) != null){ //對取出的字符串中的數字進行去重 hs.add(line.charAt(0)); //拼接顯示初始狀態的字符串 startStr += line.charAt(0) + " "; } //去除首尾空格 System.out.println(startStr.trim()); //去重字符串 String distinctStr = "去重:"; for(char c : hs) { distinctStr += c + " "; } //去除首尾空格 System.out.println(distinctStr.trim()); //排序 ArrayList<Integer> list = new ArrayList<>(); for(char c : hs) { list.add(Integer.parseInt("" + c)); } Collections.sort(list); //打印排序字符串 String sortStr = "排序:"; for(int i : list) { sortStr += i + " "; bw.write(i + ""); bw.newLine(); } System.out.println(sortStr.trim()); br.close(); bw.close(); }
標記思想java
書寫一個類,類名爲Itheima;bootstrap
類中有一個方法,方法名either24;數組
給定一個整型數組,若是數組中含有兩個相鄰的數字2,或者是兩個相鄰的數字4,就返回true,不然返回false,
若是數組既含有兩個相鄰的數字2又含有兩個相鄰的數字4,就返回false。
例如:([1, 2, 3, 2, 2, 4, 4]) -> false函數
方法調用 指望值
either24([1,2,2]) true
either24([4,4,1]) true
either24([4,4,1,2,2]) falsecode
public class Itheima { public boolean either24(int[] arr){ //初始化標記 boolean has22 = false; boolean has44 = false; //符合條件,改變標記狀態 for(int i = 0; i < arr.length - 1; i++){ if(arr[i] == 2 && arr[i + 1] == 2){ has22 = true; } if(arr[i] == 4 && arr[i + 1] == 4){ has44 = true; } } //經過標記來判斷 if(has22 && has44){ return false; } else if((has22 && !has44) || (!has22 && has44)){ return true; } else { return false; } } }