小编Jac*_*k K的帖子

为什么ArrayList使用瞬态存储?

我正在阅读Java的ArrayList的源代码,我遇到了它的支持数组声明:

private transient Object[] elementData;
Run Code Online (Sandbox Code Playgroud)

为什么这需要是暂时的?为什么不能将这个类序列化?

谢谢您的帮助!

java arraylist transient

30
推荐指数
3
解决办法
5516
查看次数

优化Project Euler#22

提前致谢.

我刚刚解决了Project Euler#22,这个问题涉及从文件中读取大约5,000行文本,并根据字符串字符的总和以及字母顺序确定特定名称的值.

但是,代码运行大约需要5-10秒,这有点烦人.优化此代码的最佳方法是什么?我目前正在使用扫描仪将文件读入字符串.还有另一种更有效的方法吗?(我尝试使用BufferedReader,但这甚至更慢)

public static int P22(){


    String s = null;

    try{
        //create a new Scanner to read file
        Scanner in = new Scanner(new File("names.txt"));
        while(in.hasNext()){
            //add the next line to the string
            s+=in.next();
        }

    }catch(Exception e){

    }
    //this just filters out the quotation marks surrounding all the names
    String r = "";
    for(int i = 0;i<s.length();i++){
        if(s.charAt(i) != '"'){
            r += s.charAt(i);
        }
    }
    //splits the string into an array, using the commas separating each name …
Run Code Online (Sandbox Code Playgroud)

java optimization file-io

2
推荐指数
1
解决办法
1308
查看次数

标签 统计

java ×2

arraylist ×1

file-io ×1

optimization ×1

transient ×1