小编Kon*_*kov的帖子

java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Long

我创建了这个用于读取Linux正常运行时间的简单示例:

public String getMachineUptime() throws IOException {
    String[] dic = readData().split(" ");

    long s = (long) Array.get(dic, 1);
    return calculateTime(s);
}

private String readData() throws IOException {
    byte[] fileBytes;
    File myFile = new File("/proc/uptime");
    if (myFile.exists()) {
        try {
            fileBytes = Files.readAllBytes(myFile.toPath());
        } catch (java.nio.file.AccessDeniedException e) {
            return null;
        }

        if (fileBytes.length > 0) {
            return new String(fileBytes);
        }
    }
    return null;
}

private String calculateTime(long seconds) {
    int day = (int) TimeUnit.SECONDS.toDays(seconds);
    long hours = TimeUnit.SECONDS.toHours(seconds)
            - TimeUnit.DAYS.toHours(day);
    long …
Run Code Online (Sandbox Code Playgroud)

java

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

在Java HashMap中使用对象作为键

package test;

import java.util.HashMap;

public class test {
    public static void main(String args[]) {
        HashMap<ID, String> h = new HashMap<ID, String> ();
        String b;

        ID id1 = new ID(100);
        ID id2 = new ID(200);
        ID id3 = new ID(200);

        h.put(id1, "apple");
        h.put(id2, "pear");

        **System.out.println(h.containsKey(id3));**
    }
}

class ID {
    Integer code;

    public ID(Integer id) {
        this.code = id;
    }

    @Override
    public int hashCode()
    {
        return code.hashCode();
    }

    @Override
    public boolean equals(Object o)
    {
        return this.code.equals(o);
    }
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出是"假".我无法理解这个结果.我实现了hashCode和equals,它们被覆盖了.我不知道如何解决.如何在java HashMap中使用对象作为键?

java

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

在一段时间内将ArrayList添加到TreeMap

我正在从数据库中的表中检索数据并将整行添加到a TreeMap.

我正在使用以下代码:

ArrayList<String> listaValores = new ArrayList();
TreeMap<Integer, ArrayList> tmValores = new TreeMap<>();
Run Code Online (Sandbox Code Playgroud)

(......)

    while (rs.next()) 
    {
        listaValores.clear();
        for(int i=2; i<=ncolunas; i++)
        {
            listaValores.add(rs.getString(i));
        }
        tmValores.put(rs.getInt(1), listaValores);   
    }
Run Code Online (Sandbox Code Playgroud)

我的TreeMap键被很好地插入,但是值总是作为SELECT*FROM Table查询的结果重复为最后一行的值.

所以,如果我的表是:

id | name | last name | 
1  | joe  | lewis     | 
2  | mark | spencer   | 
3  | mike | carter    | 
4  | duke | melvin    |
Run Code Online (Sandbox Code Playgroud)

我的TreeMap包含:

1=> duke, melvin  
2=> duke, melvin  
3=> duke, melvin  
4=> duke, …
Run Code Online (Sandbox Code Playgroud)

java arraylist treemap

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

如何在 Java 中将 String[] 数组转换为 InputStream

我有一个String[]数组,我需要将其转换为InputStream.

我见过Byte[]->InputStreamString-> InputStream,但没有见过这个。有小费吗?

java arrays string inputstream

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

尝试使用join()方法

我有以下代码

class Multi extends Thread{  

 public void run(){  
  for(int i=1;i<=3;i++){  

  System.out.println(i + ": "+Thread.currentThread().getName());  
  }  
 }  
public static void main(String args[]){  
 Multi t1=new Multi();
 t1.setName("First Thread");
 Multi t2=new Multi();
 t2.setName("Second Thread");
 Multi t3=new Multi();  
 t3.setName("Third Thread");
 t1.start();  
 t2.start();  
 try{  
  t1.join();
  t2.join();
 }catch(Exception e){System.out.println(e);}  

 t3.start();  
 }  
}
Run Code Online (Sandbox Code Playgroud)

**

每次运行时输出都会有所不同:

1st time Output

1: Second Thread
2: Second Thread
3: Second Thread
1: First Thread
2: First Thread
3: First Thread
1: Third Thread
2: Third Thread
3: Third Thread


=============================================
2nd …
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

我无法使用自己的注释

使用我自己的注释时出错,任何人都可以帮助我解决这个问题这是我的以下注释:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(value=ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)


public @interface CanRun {

}
Run Code Online (Sandbox Code Playgroud)

这是我的班级之一,代替注释显示错误,任何人都可以解释和解决...

import java.lang.reflect.Method;

public class AnnotationRunner {

    public void method1() {
        System.out.println("method1");
    }

    @CanRun
    public void method2() {
        System.out.println("method2");
    }

    @CanRun
    public void method3() {
        System.out.println("method3");
    }

    public void method4() {
        System.out.println("method4");
    }

    public void method5() {
        System.out.println("method5");
    }

} 
Run Code Online (Sandbox Code Playgroud)

这是使用此注释的主类

public class MyTest {

    public static void main(String[] args) {

        AnnotationRunner runner = new AnnotationRunner();
        Method[] methods = runner.getClass().getMethods();

        for (Method …
Run Code Online (Sandbox Code Playgroud)

java reflection

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

异常处理中"finally"子句的好处

作为这方面的新手,finally在异常处理中使用该子句有什么好处.或者换句话说,当最好使用它时,最好不要使用它.

我能想到的唯一一个是关闭输入/输出流......任何其他好处??? !!

java exception-handling exception finally try-catch-finally

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

getHibernateTemplate().update和getHibernateTemplate().merge有什么区别

我正在开发web项目,我希望将用户更新到数据库中.在编写代码时,我发现如果使用它会产生错误

getHibernateTemplate().update(user)"非法尝试将集合与两个打开的会话相关联";

但如果我尝试使用getHibernateTemplate().merge(user);它完美地工作得很好.这两者有什么区别?什么是开放会话的确切含义?

java spring hibernate

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

如何将类范围的类型参数限制为原始包装器(数字)和字符串?

如何在泛型类中限制为原始包装类型(Integer,Long等)或String?像这样的东西(伪代码):

public class GroupNotificationService<String OR T extends Number>
Run Code Online (Sandbox Code Playgroud)

那可能吗?谢谢.

java generics

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

如何在Stack上实现函数isFull()的异常

import java.util.*;
import java.lang.Iterable; 

public class MyStackArray <Item> implements Iterable<Item> {
private Item I[];
        private int top;
        private int size;
        private final static int DEFAULT_SIZE = 10;

public MyStackArray () {
        this(DEFAULT_SIZE);
}

public MyStackArray (int capacity) {
        size = capacity;
        I = (Item[]) new Object [capacity];
        top = -1;
}

public Item getTop() {
        if (isEmpty())
            return null;
        return I[top];
}

public boolean isEmpty() {
        return (top == -1);
}

public boolean isFull() {
        return (top == I.length - …
Run Code Online (Sandbox Code Playgroud)

java generics stack

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