小编Abu*_*kar的帖子

将HashSet与用户类Employee一起使用

我知道这听起来是一个非常愚蠢的问题,但是在我所知道的HashSet以及在执行下面代码时看到的内容后,我感到困惑.

我有一个Employee类如下(只保留相关的代码段):

public class Employee {
    //assume it has 3 variable name(String),salary(double) and id(int)
    //assume the constructor, getter-setters are there 

    //following is my equals and hashCode implementation
    public boolean equals(Employee e){
        return name.equals(e.name);
    }

    public int hashCode(){
        return id;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我有以下代码使用HashSet:

Employee e1  = new Employee("Abc", 2.0, 1);
Employee e2  = new Employee("abc", 3.0, 4);
Employee e3  = new Employee("XYZ", 4.0, 3);
Employee e4  = new Employee("Mno", 5.0, 2);
Employee e5  = new Employee("Abc", 77.0, …
Run Code Online (Sandbox Code Playgroud)

java equals set hashcode hashset

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

带接口的匿名类

在处理匿名内部类时,我对接口的概念感到困惑.据我所知,您无法在JAVA中实例化接口,因此以下语句会产生编译错误

     ActionListener action = new ActionListener();  // compile error
Run Code Online (Sandbox Code Playgroud)

但是当它处理匿名类时会发生什么?为什么允许使用新的?例如

     JButton button = new JButton("A");
     button.addActionListener(new ActionListener(){    //this is fine
           @Override
           public void actionPerformed(ActionEvent e){

           }
     };
Run Code Online (Sandbox Code Playgroud)

编译器只是ActionListener在场景后面创建一个类并实现吗?它是如何工作的 ?

java interface anonymous-function inner-classes

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

为什么String.split("$")不起作用?

我有三个部分字符串,每个部分用$ 符号分隔
例如,

String s = "abc$efg$xyz";
Run Code Online (Sandbox Code Playgroud)

现在当我使用这样的split方法拆分它时:

String values[] = s.split("$");
Run Code Online (Sandbox Code Playgroud)

它values数组包含整个字符串作为单个元素.

但是当我使用它时:

String values[]=s.split("\\$");
Run Code Online (Sandbox Code Playgroud)

它完美的作品是什么,我想这意味着
现在的values数组包含abc,efg并xyz分别对指数0,1和2.

我想知道为什么第一次split不起作用,因为我split在分裂a single white space使用时使用了类似的东西split(" ");

java string split

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

无法隐式转换类型System.Collections.Generic.IList System.Collections.Generic.List

基本上我正在将SQL数据表转换为我可以成功完成的通用列表.但我无法弄清楚如何返回列表对象.我得到一个错误,说是一个变体 - 无法隐式转换类型System.Collections.Generic.IList到System.Collections.Generic.List

将List作为类型List返回的正确方法是什么?

public List<MyObject> GetAllMyObjects()
{
    String command = "select * from names ";
    DataTable tableResult = DataTableToList.ExecuteDataTable(command, CommandType.Text, null,connectionString);

    IList<MyObject> theList = DataTableToList.ConvertTo<MyObject>(tableResult);

    return // I’m not sure how to return theList here...
}
Run Code Online (Sandbox Code Playgroud)

c# generics list

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

乔达时间 DateTimeZone

我在浏览joda 网站的时区页面时发现了这个

-05:00  EST   
-05:00  EST5EDT   
-05:00  Etc/GMT+5   
Run Code Online (Sandbox Code Playgroud)

我只是想知道这是什么EST5EDT意思,我通常使用EST所以只是想知道它。

在发布问题之前我已经搜索过并没有发现任何内容。

java timezone jodatime

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

为什么当我在src属性的相同脚本标签中使用jquery/javascript代码时,jquery/javascript代码不起作用

可能重复:
带有src和内容的脚本标记是什么意思?

我在我的html页面中使用了以下内容

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="resources/scripts/jquery-1.8.2.min.js">

$(document).ready(function(){
  alert("ABC");
});
</script>
</head>

<body>
<p>THIS IS MY INDEX PAGE.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

jquery在这里不起作用,这意味着我在其中发出警报后看不到它的任何影响.

但是,当我<script>按照下面的标签放入单独的标签时,

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="resources/scripts/jquery-1.8.2.min.js">
</script>
<script>
$(document).ready(function(){
  alert("ABC");
});
</script>
</head>

<body>
<p>If you click on me, I will disappear.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

所以,如果有人解释?

html javascript jquery

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

如何根据我输入的字符获得if else操作

我试图获得一个if else if语句,但在使用它时遇到错误,请参阅下面的程序谢谢

public class Info {

  public static void main(String [] args){
    Scanner input=new Scanner(System.in);
    System.out.print("Enter employee num");
    int e_num=input.nextInt();
    System.out.print("Enter employee first name");
    String e_fname=input.next();
    System.out.print("Enter employee surname");
    String e_sname=input.next();
    System.out.print("Enter employee code C or c,H or h and F or f");
    char e_code = input.next().charAt(0);

   if(e_code==F||e_code==f){
       e_code==monthly_paid;
   }            
 }
}
Run Code Online (Sandbox Code Playgroud)

java

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

使用超级关键字迭代通用列表

我有以下2课:

class A {
    public String toString(){
        return "A Class";
    }
}

class B extends A {
    public String toString(){
        return "B Class";
    }
}
Run Code Online (Sandbox Code Playgroud)

我有以下代码,在将一些对象添加到列表后调用测试方法:

List<B> myList = new ArrayList<B>();
myList.add(new B());
myList.add(new B());

testList(myList);
Run Code Online (Sandbox Code Playgroud)

现在,我的testList方法是这样的:

public void testList(List<? super B> aList){        
        for(A myObject : aList){  //This is showing compiler error
            System.out.println(myObject);
        }  

       for(B myObject : aList){  //Even this is showing compiler error
            System.out.println(myObject);
        }

       for(Object myObject : aList){  //This works fine
            System.out.println(myObject);
        }
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能使用参考类型A …

java generics collections

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

我的代码有什么问题(初学者)(Java)

我正在做一些Java只是为了练习,并决定做一个Dungeons&Dragons类型的代码.(这只是角色统计数据)

我输入了所有内容,它一直说(角色)最伟大的数据是生命力.救命?

import java.util.Random;
import java.util.Scanner;
public class DAndD {

    public static void main(String[] args) {
         // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        Random generator = new Random();
        System.out.println("Choose a name for your Dungeons & Dragons       character.");
        String character = input.next();
        System.out.print("Ah, " + character + " has a nice ring to  it.\nLet's roll for stats shall we?");
        int str = generator.nextInt(21);
        int dex = generator.nextInt(21);
        int intel = generator.nextInt(21);
        int wis = generator.nextInt(21);
        int vit = …
Run Code Online (Sandbox Code Playgroud)

java

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