JAVA finds the number of characters in the text and sorts

JAVA object-oriented questions (including a little input and output stream)

Topic:
Count the number of occurrences of relevant characters in a text file calcCharNum.txt, such as: middle (8), b(16), +(10), etc. The characters outside the parentheses represent the number of occurrences of the characters. It is required to use a custom class to encapsulate characters and the number of occurrences, as well as the operations related to the custom class encapsulation, and output from largest to smallest after sorting by the number of occurrences


The code is as follows (example):

public class CharNum implements Comparable<CharNum> {
    
           //实现compare 接口,如果不懂这个 的意死,可以自行百度
	private int num = 1;     //表示各个字符出现的个数
	private char c;  //定义变量字符 存储 文本内的汉字、英文字母等、
	//构造函数
	public CharNum(char c) {
    
    
		
		this.c = c;
	}
	
	//set/get方法
	public int getNum() {
    
    
		return num;
	}
	public void setNum(int num) {
    
    
		this.num = num;
	}
	public char getC() {
    
    
		return c;
	}
	public void setC(char c) {
    
    
		this.c = c;
	}

	@Override
	public String toString() {
    
    
		return "("+c+")"+"出现的次数为:"+num;
	}

	@Override
	public int compareTo(CharNum o) {
    
          //实现接口compare中的方法
		if(this.num == o.num ) {
    
    
			return this.c - o.getC();
		}else {
    
    
			return this.num - o.num;
		}
	}
}


public class CharArray {
    
    
	private static CharNum[] arr = new CharNum[0];

	private CharArray() {
    
    }

	private static CharArray instance = new CharArray();

	public static CharArray getinstance() {
    
    
		return instance;
	}

	public void add(char c) {
    
    
		if (c == '\n' || c == '\r')
			return;
		boolean exist = false;
		int i = 0;
		for (; i < arr.length; i++) {
    
    
			CharNum temp = arr[i];
			if (temp == null)
				break;
			if (c == temp.getC()) {
    
    
				arr[i].setNum(arr[i].getNum() + 1);
				exist = true;
				break;
			}
		}

		if (!exist) {
    
    
			if (i >= arr.length) {
    
    
				addLength(c);
			}
			CharNum temp = new CharNum(c);
			arr[i] = temp;
		}
	}

	private void addLength(char c) {
    
    
		// TODO Auto-generated method stub
		int len = arr.length * 2 + 1;
		CharNum[] arr1 = new CharNum[len];
		for (int i = 0; i < arr.length; i++) {
    
    
			arr1[i] = arr[i];
		}
		arr = arr1;
	}

	public void sort() {
    
    
		for (int i = 1; i < arr.length; i++) {
    
    
			for (int k = 0; k < arr.length - i; k++) {
    
    
				if(arr[k]!=null&&arr[k+1]!=null) {
    
    
				if (arr[k].compareTo(arr[k + 1]) < 0) {
    
    
					CharNum temp = arr[k];
					arr[k] = arr[k + 1];
					arr[k + 1] = temp;
				}
				}
			}
		}
	}

	public String toString() {
    
    
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
    
    
			if (arr[i] != null) {
    
    
				sb.append(arr[i].toString() + ",");
				if ((i + 1) % 5 == 0)
					sb.append("\n");
			}
		}
		if(sb.length()>1) {
    
    
			sb.deleteCharAt(sb.length() - 1);
		}
		return sb.toString();
		

	}

}



import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class CharTest {
    
    
	public static void main(String[] args) throws IOException {
    
    
		File f = new File("aaa.txt");
		CharArray arr = CharArray.getinstanct();
		if(f.exists()) {
    
    
			try(Reader r = new BufferedReader(new FileReader(f));){
    
    
				int cc;
				while((cc = r.read())!=-1) {
    
    
					arr.add((char)(cc));
				}
			}
			arr.sort();
			System.out.println(arr);
		}
	}
}

Guess you like

Origin blog.csdn.net/weixin_42437438/article/details/113620532