数据结构这块越学越敬佩 博大精深
统计大文件中相同年龄的人的个数
public static void main(String[] args) throws Exception {
String str = "";
String fileName = "";
InputStreamReader isr = new InputStreamReader(
new FileInputStream(fileName), StandardCharsets.UTF_8);
long start = System.currentTimeMillis();
BufferedReader br = new BufferedReader(isr);
int tot = 0;
int[] data = new int[200];
while ((str = br.readLine()) != null) {
int age = Integer.parseInt(str);
//巧用下标
data[age]++;
tot++;
}
System.out.println("总共数据大小" + tot);
for (int i = 0; i < 200; i++) {
System.out.println(i + ":" + data[i]);
}
System.out.println("花费时间" + (System.currentTimeMillis() - start) + "ms");
}
private static final int DEFAULT_SIZE = 10;
private Object data[];
private int index;
private int size;
public MyArrayList() {
this.data = new Object[DEFAULT_SIZE];
this.size = DEFAULT_SIZE;
}
@Override
public void add(E e) {
data[index++] = e;
if (index == size) { // ����
this.size = this.size * 2 + 1;
Object newData[] = new Object[this.size];
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
this.data = newData;
}
}
@Override
public void remove(int i) {
if (i >= 0 && i < index) {
for (int j = i; j < this.data.length - 1; j++) {
data[j] = data[j + 1];
}
this.index--;
}
}
@Override
public E get(int i) {
if (i >= 0 && i < index) {
return (E) this.data[i];
}
return null;
}
@Override
public int size() {
return index;
}
@Override
public boolean isEmpty() {
if(index <= 0) return true;
return false;
}
@Override
public void remove(Object e) {
if(!isEmpty()) {
for(int i = 0 ; i < this.data.length ; i ++) {
if(data[i].equals(e)) {
remove(i);
break;
}
}
}
}
private int size;
private int data[];
private int index;
public ArrayTest(int size) {
index = 0;
this.size = size;
data = new int[size];
}
public void print() {
System.out.println("index" + index);
for (int i = 0; i < index; i++) {
System.out.println(data[i] + "");
}
System.out.println();
}
public void insert(int loc, int n) {
if (index++ < size) {
for (int i = size - 1; i > loc; i--) {
data[i] = data[i - 1];//数据后移
}
data[loc] = n;
} else {
}
}
public void delete(int loc) {
for (int i = loc; i < size; i++) {
if (i != size - 1) {
data[i] = data[i + 1];
} else {
data[i] = -1;
}
}
index--;
}
public class MyLinkedList {
private ListNode head;
private int size = 0;
public void insertHead(int data) {
ListNode newNode = new ListNode(data);
//先把后面的连上 再连前面的 重要! 先从尾开始,链表先从pre前开始
newNode.next = head;
head = newNode;
}
public void insertNth(int data, int position) {
if (position == 0) {
insertHead(data);
} else {
ListNode cur = head;
//找到位置先
for (int i = 1; i < position; i++) {
cur = cur.next;
}
ListNode newNode = new ListNode(data);
//猎杀时刻!
newNode.next = cur.next;
cur.next = newNode;
}
}
public void deleteHead() {
head = head.next;
}
public void deleteNth(int position) {
if (position == 0) {
deleteHead();
} else {
ListNode cur = head;
for (int i = 1; i < position; i++) {
cur = cur.next;
}
cur.next = cur.next.next;
}
}
public void find(int data){
ListNode cur = head;
while (cur !=null){
if(cur.value==data) break;
cur = cur.next;
}
}
}
class ListNode {
int value;
ListNode next;
ListNode(int value) {
this.value = value;
this.next = null;
}
}
/**
* //LRU
* //遍历,找到 删除 插入头部,最新
* //不在 有空间 插入头部 没有空间 删除最后一个
*/
public class DoubleLinkList { // 双向链表
private DNode head; //头
private DNode tail; // 尾
DoubleLinkList(){
head = null;
tail = null;
}
public void insertHead(int data){
DNode newNode = new DNode(data);
if(head == null){
tail = newNode;
}else{
head.pre = newNode;
newNode.next = head;
}
head = newNode;
}
public void deleteHead(){
if(head == null) return ; //没有数据
if(head.next == null){ //就一个点
tail = null;
}else{
head.next.pre = null;
}
head = head.next;
}
public void deleteKey(int data){
DNode current = head;
while (current.value != data) {
if (current.next == null) {
System.out.println("没找到节点");
return ;
}
current = current.next;
}
if (current == head) {// 指向下个就表示删除第一个
deleteHead();
} else {
current.pre.next = current.next;
if(current == tail){ //删除的是尾部
tail = current.pre;
current.pre = null;
}else{
current.next.pre = current.pre;
}
}
}
}
class DNode{
int value; //值
DNode next; //下一个的指针
DNode pre; //指向的是前一个指针
DNode(int value){
this.value = value;
this.next = null;
this.pre = null;
}
}
public class ArrayStack<Item> implements MyStack<Item> {
private int num = 0;
private Item[] items = (Item[]) new Object[1];
@Override
public MyStack<Item> push(Item item) {
judgeSize();
items[num++] = item;
return null;
}
public ArrayStack(int cap) {
items = (Item[]) new Object[cap];
}
private void judgeSize() {
if (items.length <= num) {
resize(2 * items.length);
} else if (num > 0 && num <= items.length / 2) {
resize(items.length / 2);
}
}
private void resize(int size) {
Item[] temp = (Item[]) new Object[size];
for (int i = 0; i < num; i++) {
temp[i] = items[i];
}
items = temp;
}
@Override
public Item pop() {
if (isEmpty()) {
return null;
}
Item item = items[--num];
items[num] = null;
return item;
}
@Override
public int size() {
return num;
}
@Override
public boolean isEmpty() {
return num == 0;
}
}
/**
* [{({([({})])})}] 是否对称
**/
public static boolean isOK(String s) {
MyStack<Character> brackets = new ArrayStack<Character>(20);
char c[] = s.toCharArray();
Character top;
for (char x : c) {
switch (x) {
case '{':
case '(':
case '[':
brackets.push(x);
break;
case '}':
top = brackets.pop();
if (top != null && top == '{') {
break;
} else {
return false;
}
case ')':
top = brackets.pop();
if (top != null && top == '(') {
break;
} else {
return false;
}
case ']':
top = brackets.pop();
if (top != null && top == '[') {
break;
} else {
return false;
}
default:
break;
}
}
//进出对称 最后没有元素 都pop了
return brackets.isEmpty();
}
public class ArrayQueue {
private final int[] data;//数据
private int head;//头
private int tail;//尾
private int n = 0;//大小
public ArrayQueue(int cap) {
data = new int[cap];
n = cap;
}
/**
* 入队列
*
* @param m
*/
public void push(int m) {
if (tail == n)
//满了
return;
data[tail] = m;
tail++;
}
/**
* 出队列
*
* @return
*/
public int pop() {
if (isEmpty()) return -1;
int m = data[head];
head++;
return m;
}
public boolean isEmpty() {
return head == tail;
}
}
//循环
public class CircleArrayQueue {
private final int[] data;//数据
private int head;//头
private int tail;//尾
private int n = 0;//大小
public CircleArrayQueue(int cap) {
data = new int[cap];
n = cap;
}
/**
* 入队列
*
* @param m
*/
public void push(int m) {
if ((tail + 1) % n == head)
//满了,头尾相连
return;
data[tail] = m;//尾插
tail = (tail + 1) % n;//后移
}
/**
* 出队列
*
* @return
*/
public int pop() {
if (isEmpty()) return -1;
int m = data[head];
head = (head + 1) % n;
return m;
}
/**
* 首尾重叠
*
* @return
*/
public boolean isEmpty() {
return head == tail;
}
}
数据同步架构参考