小编Joh*_*now的帖子

文件读取期间执行速度差异很大

谁能解释为什么会这样?文件大小最多为2MB.执行代码所需的时间不到2秒.

try {
    while ((line = br.readLine()) != null) {
        System.out.println(line);
}
catch(Exception e)
{           
}
Run Code Online (Sandbox Code Playgroud)

但是当我将代码更改为:

String temp = "";
try {
    while ((line = br.readLine()) != null) {
        temp =temp + line;
}
catch(Exception e)
{
}
Run Code Online (Sandbox Code Playgroud)

我知道这需要相对更多的时间,但需要470秒的大量时间.为何如此区别?

java file

3
推荐指数
1
解决办法
94
查看次数

将一个对象的值存储到另一个对象中

我正在尝试以下代码: -

HashMap<Integer,Integer[]> possibleSeq = new HashMap<Integer,Integer[] >();
        possibleSeq.put(0,new Integer[]{1,4,5});
        possibleSeq.put(1,new Integer[]{0,4,5,2,6});
        Integer[] store = possibleSeq.get(0);
        for(int i=0;i<store.length;i++)
        {
            System.out.print(store[i] + ' ');
        }
Run Code Online (Sandbox Code Playgroud)

输出是: -

333637
Run Code Online (Sandbox Code Playgroud)

但由于我等同值时,关键是0到变量,我打算让1 4 5作为output.So我推断Integer[] store = possibleSeq.get(0);这是不存储的元素的适当方式possibleSeq.get(0)商店我应该怎么做.所以?

java hashmap

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

键为整数,值为IntegerArray

我有以下HashMap: -

HashMap<Integer,Integer[]> possibleSeq = new HashMap<Integer,Integer[] >();
Run Code Online (Sandbox Code Playgroud)

我想在地图中添加如下内容: -

 possibleSeq.put(1,{1,2,3,4});
Run Code Online (Sandbox Code Playgroud)

有大量的条目,我应该手动输入: - 我试过这样做: -

Integer a = 1;
Integer aArr = {1,2,3,4};
  possibleSeq.put(a,aArr);
Run Code Online (Sandbox Code Playgroud)

但这不是我的要求.我不想创建单独的Integer变量来存储键和单独的Integer Arrays来存储我的值,即IntegerArray.任何想法?

java hashmap map

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

循环空指针异常

如果newWordnull,它不应该进入循环,但为什么它进入循环并给出java.lang.NullPointerException

newWord = "abcd";
while(!newWord.equals(null))
{
    try {
    newWord = br.readLine();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    }
    catch(NullPointerException p)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

它给出了堆栈跟踪,但我没有在任何地方使用printStackTrace()

java exception-handling nullpointerexception while-loop

0
推荐指数
1
解决办法
1501
查看次数