Java工具类

我的意思是我要陆续写一些Java的代码,方便做一些琐碎的任务,比如:读写文本文件,直接将他写成方法,直接调用就好了,提高一点开发效率,开发环境是 JavaSE-9 版本。这个要不定时更新了。

读文件

读文件,把文本文件内容按行存入到List集合中,我们只要输入文件路径(一个字符串),就可以读取出所有内容了,用循环就可以按行读取出List里的内容了(所有实现List接口的集合类都可以作为输出)。

/**
     * 读文件,把文件内容返回到一个字符串列表中
     * @param filePath 文件的路径
     * @return 文件中的所有内容
     */
    public static List<String> read(String filePath) {
        List<String> allLine = new ArrayList<String>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
            String line = "";
            while ((line = br.readLine())  != null) {
                allLine.add(line);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return allLine;
    }

写文件

    /**
     * 将内容写入到文件
     * @param list 写入的内容
     * @param filePath 文件路径
     */
    public static void write(final List<String> list, String filePath) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
            int i = 0;
            for (String line : list) {
                bw.append(line);
                i++;
                if (i < list.size())
                    bw.newLine();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }       
    }

队列

实现队列时遇到了一个问题,就是实现 获取内部元素的方法content 时,不能将object转化为特定类型(如:Integer、Double等)。造成这个问题的原因为泛型数组不能直接实例化,只能引用Object等具体类型的数组。一开始准备使用反射来解决这个问题,但是没有解决。我这里是获取到运行时Item的对应类型名,然后创建临时的类型对象,最后将引用知道这个临时对象即可。 toString()方法帮了我很大的忙,具体请看content方法。

但是理论上,队列只关心进出队列的顺序逻辑和进出队列的元素。并不关心中间的元素,也不需要获取所有的元素,这和栈相似。不过,解决这个刁钻的问题也很有必要,这样队列更加灵活,能应付更复杂的逻辑。

printContent()方法和content()方法的区别是:前者只负责打印,后者可以获取到所有的元素。

理论上我希望这个队列可以放入任何对象,实际上它目前只能放入int、double和String类型的数据,它可以扩展加入其他类型数据,但要可以放入所有类型的数据,几乎是不可能的。 如果有大神可以解决这个问题,请告诉我,大家互相学习,谢谢

/**
* @author 龙卫兵
*/
public class MyQueue<Item> {
    private Object[] myArray;
    private int capacity;  // 容量
    private int n;  // 元素个数

    public MyQueue() {
        capacity = 2;
        myArray = new Object[capacity];
    }

    public MyQueue(int capacity) {
        this.capacity = capacity;
        myArray = new Object[capacity];
    }

    public void push(Item item) {
        if (n > capacity/2) {
            capacity *= 2;
            Object[] temp = new Object[capacity];
            for (int i = 0; i < capacity/2; i++)
                temp[i] = myArray[i];
            myArray = temp;
        }       
        myArray[n++] = item;
    }

    public void pop() {
        if (isEmpty())
            throw new NullPointerException("数组为空");
        else {
            for (int i = 0; i < n-1; i++) {
                myArray[i] = myArray[i + 1];
            }
            myArray[--n] = null;
        }
    }

    public int size() {
        return n;
    }

    public int getCapacity() {
        return capacity;
    }

    public boolean isEmpty() {
        return n == 0;
    }

    public void printContent() {
        for (int i = 0; i < n; i++) {
            System.out.println(myArray[i]);
        }
    }

    public Item[] content() {
        String itemName = myArray[0].getClass().getSimpleName();
        if (itemName.equals("Integer")) {
            Integer[] temp = new Integer[n];
            for (int i = 0; i < n; i++) {
                temp[i] = new Integer(myArray[i].toString());
                // System.out.println(temp[i]);
            }
            return (Item[])temp;
        } else if (itemName.equals("String")) {
            String[] temp = new String[n];
            for (int i = 0; i < n; i++) {
                temp[i] = myArray[i].toString();
                // System.out.println(temp[i]);
            }
            return (Item[])temp;
        } else if (itemName.equals("Double")) {
            Double[] temp = new Double[n];
            for (int i = 0; i < n; i++) {
                temp[i] = Double.valueOf(myArray[i].toString());
                // System.out.println(temp[i]);
            }
            return (Item[])temp;
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println("**********************元素为int********************************");
        MyQueue<Integer> queue1 = new MyQueue<Integer>();
        System.out.println("The first capacity of queue1 is " + queue1.getCapacity());
        queue1.push(1);
        queue1.push(2);
        queue1.push(3);
        System.out.println("The second capacity of queue1 is " + queue1.getCapacity());
        System.out.println("The size of queue1 is " + queue1.size());
        queue1.printContent();
        System.out.println("**********************删除一个元素后********************************");
        queue1.pop();
        queue1.printContent();
        System.out.println("**********************删除一个元素后********************************");
        queue1.pop();
        queue1.printContent();
        System.out.println("**********************添加一个元素后********************************");
        queue1.push(16);
        queue1.printContent();
        //Integer[] in = (Integer[])queue1.content();
        Integer[] m = queue1.content();
        for (int i = 0; i < queue1.size(); i++) {
            System.out.println(m[i]);
        }
        System.out.println("**********************************************************");
        MyQueue<Integer> queue2 = new MyQueue<Integer>(5);
        System.out.println("The first capacity of queue2 is " + queue2.getCapacity());
        queue2.push(4);
        queue2.push(5);
        queue2.push(6);
        System.out.println("The second capacity of queue2 is " + queue2.getCapacity());
        System.out.println("The size of queue2 is " + queue2.size());
        queue2.printContent();
        System.out.println("********************元素为字符串******************************");
        MyQueue<String> queue3 = new MyQueue<String>();
        queue3.push("nihao");
        queue3.push("haha");
        queue3.push("zdjd");
        String[] mm = queue3.content();
        for (int i = 0; i < queue3.size(); i++) {
            System.out.println(mm[i]);
        }
        System.out.println("********************元素为double******************************");
        MyQueue<Double> queue4 = new MyQueue<Double>();
        Double x1 = new Double("5.666");
        queue4.push(x1);
        Double x2 = new Double("-5.666");
        queue4.push(x2);
        Double[] mmm = queue4.content();
        for (int i = 0; i < queue4.size(); i++) {
            System.out.println(mmm[i]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值