利用scanner函数进行的数据的读入模板(1)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* 平时写程序一般不用Scanner,线上笔试的时候,各大公司热衷于Scanner输入。
* 平时用LeetCode刷题也不会用到,结果多次在笔试时候卡在Scanner,特来总结一波。
*/
public class IO_template {
public static void main(String[] args) {
}
/**
* 多行输入元素,其中第一行几个数字表示下面几行的个数。
* // 输入如下
* 输入的数据分别表示的是的数组的大小
* 3 4
* 10 2 3
* 11 4 5 6
*/
public static void test1() {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int m = sc.nextInt();
int n = sc.nextInt();
int[] num1 = new int[m];
int[] num2 = new int[n];
for (int i = 0; i < m; i++) {
num1[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
num2[i] = sc.nextInt();
}
//显示结果的代码
System.out.println("输出:");
System.out.println(Arrays.toString(num1));
System.out.println(Arrays.toString(num2));
}
}
/**
* 在一行输入多个参数
* ABB CCC DDD EEE 123 435
*/
public static void test2() {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine(); // 读取一行
String[] strIn = str.trim().split(" "); // 以空格分割
System.out.println(Arrays.toString(strIn));//显示结果
}
}
/**
* java中从控制台输入多行数据 按回车键输入空行结束
* 4
* 5
* 6
* 空格
*/
public static void test5() {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
do {
//读取到的每一行的字符串
String string = sc.nextLine();
if (string.equals("")) {
break;
}
list.add(Integer.valueOf(string));
} while (true);
//显示结果
for (int v : list) {
System.out.println(v);
}
}
利用scanner函数进行的数据的读入模板(2)
/** 这样是有非法信息的输入 怎么防止的数据的正常输入
* 有异常的输入结果 1, 2, 3
*/
private static void IO() {
Scanner sc = new Scanner(System.in);
String oldstr = sc.nextLine().trim();
String str = "";
for (int i = 0; i < oldstr.length(); i++) {
if (oldstr.charAt(i) != ' ') {
str += oldstr.charAt(i);
}
}
System.out.println(str);
}
利用scanner函数进行的数据的读入模板(3)
/**
* 放置非法的输入符号问题
* 输入的是 [1,2,3,4,5],[5,6,7,8,9]
*/
private static void IO1() {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().trim();
String ans = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ']' || str.charAt(i) == '[') {
continue;
}
ans += str.charAt(i);
}
String[] split = ans.split(",");
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
for (int i = 0; i < split.length / 2; i++) {
list1.add(Integer.valueOf(split[i]));
}
for (int i = split.length / 2; i < split.length; i++) {
list2.add(Integer.valueOf(split[i]));
}
System.out.println(list1.toString());
System.out.println(list2.toString());
}
利用scanner函数进行的数据的读入模板(4)
新的代码输入输出模板 采用的是字符流
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
//读取一行进行切割
String[] split = bf.readLine().split(",");
//读取一行进行的类型的转换
long n = Long.valueOf(bf.readLine());
for (String S : split) {
System.out.print(S + " ");
}
System.out.println();
System.out.println(n + " ====");
}
利用scanner函数进行的数据的读入模板(5)
/**
* 采用功能的是的 BufferedReader
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//读取的一行 其中的是表示的数字
int N = Integer.valueOf(br.readLine().trim());
String[] values = br.readLine().trim().split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
sb.append(Integer.valueOf(values[i])).append(" ");
}
System.out.println(sb.toString().trim());
}
}
hashmap按照value的值的进行排序
public static void test1() throws Exception {
// 创建一个字符串为Key,数字为值的map
Map<String, Integer> map = new HashMap<>();
map.put("clothes", 120);
map.put("grocery", 150);
map.put("transportation", 100);
map.put("utility", 130);
map.put("rent", 1150);
map.put("miscellneous", 90);
System.out.println("______________________排序前___________________________ ");
for (String key : map.keySet()) {
System.out.println(key + "--" + map.get(key));
}
System.out.println("______________________//按值升序排序后___________________________ ");
Map<String, Integer> sorted = map
.entrySet()
.stream()
.sorted(comparingByValue())
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,LinkedHashMap::new));
for (String key:sorted.keySet()){
System.out.println(key + "--" + sorted.get(key));
}
System.out.println("______________________按值排序降序排序后___________________________ ");
sorted = map
.entrySet()
.stream()
.sorted(Collections.reverseOrder(comparingByValue()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
for (String key:sorted.keySet()){
System.out.println(key + "--" + sorted.get(key));
}
}
hasmap按照key value的值进行的排序
public static void test() {
Map<String, Integer> map = new HashMap();
map.put("Apple", 7299);
map.put("SAMSUNG", 6000);
map.put("Meizu", 2698);
map.put("Xiaomi", 2400);
System.out.println("______________________//按照原始顺序排列__________________________________");
//key-sort
for (String s : map.keySet()) {
System.out.println(s + "--" + map.get(s));
}
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
System.out.println("________________________//按照的key的顺序排列_____________________________");
//value-sort
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
for (Map.Entry<String, Integer> mapping : list) {
System.out.println(mapping.getKey() + ": " + mapping.getValue());
}
System.out.println("________________________//按照的key的倒序排列_______________________________");
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
for (Map.Entry<String, Integer> mapping : list) {
System.out.println(mapping.getKey() + ": " + mapping.getValue());
}
System.out.println("________________________//按照的value的顺序排列_____________________________");
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
for (Map.Entry<String, Integer> mapping : list) {
System.out.println(mapping.getKey() + ": " + mapping.getValue());
}
System.out.println("________________________//按照的value的倒序排列__________________________");
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
for (Map.Entry<String, Integer> mapping : list) {
System.out.println(mapping.getKey() + ": " + mapping.getValue());
}
}
整数数组转list(int[] ==>list)
package Test_Pricate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
test2();
}
/**
* 整数的list--int[]
*/
public static void test() {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
Integer[] array = list.toArray(new Integer[]{0});
System.out.println(array);
}
/**
* 整数的list--Double[]
*/
public static void test2() {
ArrayList<Double> list = new ArrayList<>();
list.add(1.0);
list.add(2.0);
list.add(3.3);
list.add(4.6);
Double[] array = list.toArray(new Double[]{0.0});
//Double[] array1 = (Double[])list.toArray(); 出现不能转为的错误
for (Double s : array) {
System.out.print(s);
}
}
/**
* 整数的list--String[]
*/
public static void test3() {
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
String[] array = (String[]) list.toArray();
for (Object value : array) {
System.out.print(value);
}
}
public void test4() {
String[] array = {"A", "B", "C", "D"};
ArrayList<String> list = new ArrayList<String>(Arrays.asList(array));
int[] data = {4, 5, 3, 6, 2, 5, 1};
// int[] 转 List<Integer>
List<Integer> list0 = Arrays.stream(data).boxed().collect(Collectors.toList());
// int[] 转 List<Integer>
List<Integer> list1 = Arrays.stream(data).boxed().collect(Collectors.toList());
// int[] 转 Integer[]
Integer[] integers1 = Arrays.stream(data).boxed().toArray(Integer[]::new);
// List<Integer> 转 Integer[]
Integer[] integers2 = list1.toArray(new Integer[0]);
// List<Integer> 转 int[]
int[] arr1 = list1.stream().mapToInt(Integer::valueOf).toArray();
// Integer[] 转 int[]
int[] arr2 = Arrays.stream(integers1).mapToInt(Integer::valueOf).toArray();
// Integer[] 转 List<Integer>
List<Integer> list2 = Arrays.asList(integers1);
}
}
List转为数组(list==》int[])
package Test_Pricate;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
test2();
}
/**
* 整数的list--int[]
*/
public static void test() {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
Integer[] array = list.toArray(new Integer[]{0});
System.out.println(array);
}
/**
* 整数的list--Double[]
*/
public static void test2() {
ArrayList<Double> list = new ArrayList<>();
list.add(1.0);
list.add(2.0);
list.add(3.3);
list.add(4.6);
Double[] array = list.toArray(new Double[]{0.0});
//Double[] array1 = (Double[])list.toArray(); 出现不能转为的错误
for (Double s : array) {
System.out.print(s);
}
}
/**
* 整数的list--String[]
*/
public static void test3() {
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
String[] array = (String[]) list.toArray();
for (Object value : array) {
System.out.print(value);
}
}
}
Arraylist存储二维数据(Arraylist+int[] 或者是Arraylist+Arraylist)
/**
* 输入 是的保存是的是hashmap的存储结构 list的每一个是的int[]的数组
*/
public static void IO() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
List<int[]> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
String[] str = sc.nextLine().trim().split(" ");
int[] array = new int[str.length];
for (int j = 0; j < array.length; j++) {
array[j] = Integer.valueOf(str[j]);
}
list.add(array);
}
for (int[] array : list) {
for (int Value : array) {
System.out.print(Value + " ");
}
System.out.println();
}
}