Java 记录一些日常操作(持续更新)

数组

判断某一元素是否在数组内

  • 使用for循环 (效率高
  • 转化为List
	List<String> list = Arrays.asList(strArr);
    boolean result = list.contains(str);
    int index = list.indexOf(str); //不包含返回-1
  • 二分查找法(数字,已排序)
String[] arrays = {"1","2","3","4"};
int index = Arryas.binarySearch(arrays,"2");

转化

字符串与数组之间的转化

  1. 字符串转化为数组:
	//String转化为String[] 
    String string = "abc,def,ghi";
    String[] str = string.split(",");

	//String 转化为char[]
	private char[] finalSymbols = tempString.toCharArray();
  1. 数组转化为字符串:
String str1 = String.valueOf(array);

ArrayList 与 字符串数组转化

	// String[] 转 List<String>
    List<String> list3 = Arrays.asList(strings1);
    // String[] 转 ArrayList<String>
    ArrayList<String> list3 = new ArrayList<>(Arrays.asList(strings1));
    // List<String> 转 String[]
    String[] strings2 = list3.toArray(new String[0]);

二维数组

二维ArrayList

创建二维ArrayList:

	ArrayList<ArrayList<String>> parentremovedList = new ArrayList<ArrayList<String>>();

添加元素:

	ArrayList<String> removedList = new ArrayList<>();
	parentremovedList.add(removedList);

文件

文件读写

读取某一行文件
File file = new File(fileName);
        BufferedReader reader = null;
        try {
            //("以行为单位读取文件内容,一次读一整行")
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

对于readLine() 的说明:

假如只想读到第4行的数据,不输出第6行数据:

	// 使用equals
	while (!(tempString = reader.readLine()).equals("")) {
			System.out.println(tempString);
		}
	//不能使用=来判断(??????)
		while ((tempString = reader.readLine() != "") {
			System.out.println(tempString);
		}
相对路径读取文件

文件结构如下:
文件结构

//读取文件的路径
	File file = new File("src/lexer/test1.txt");
	FileReader reader = new FileReader(file);

字符串

判断字符串是否以某个字符开始

	String str = "admin";
	boolean b = str.startsWith("a");//true

取字符串第一个字符

	s.charAt(0)
	//也可使用subStsring(0,1)

Map

遍历

	//1.
	for (Map.Entry<Character, List<String>> entry : produce.entrySet()) {
		System.out.println(entry.getKey() + ": " + entry.getValue());
	}

优先队列(最大堆,最小堆)

PriorityQueue(优先队列)

PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(); //小顶堆,默认容量为11
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(11,new Comparator<Integer>(){ //大顶堆,容量11
    @Override
    public int compare(Integer i1,Integer i2){
        return i2-i1;
    }
});

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值