java菜鸟求教import java.util.*public class BasicContainer{public static void main(String[] args){C
java菜鸟求教
import java.util.*;
public class BasicContainer{
public static void main(String[] args){
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","11"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Interger(100));
System.out.println(c.remove(new Name("f1","11")));
System.out.println(c);
}
}
class Name{
private String firsName,lastName;
public Name(String firstName,String lastName){
this.firstName = firstName; this.lastName = lastName;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String toString(){
return firstName + " " + lastName;
}
}
问各大侠哪错了,咋改;
[最优解释]
我用eclipse给你编译运行了一下。
你这个程序一共2处错误
import java.util.*;
public class BasicContainer{
public static void main(String[] args){
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","11"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Interger(100));//多打了一个r,自己看看
System.out.println(c.remove(new Name("f1","11")));
System.out.println(c);
}
}
class Name{
private String firsName,lastName;//定义的时候少打了一个t
public Name(String firstName,String lastName){
this.firstName = firstName; this.lastName = lastName;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String toString(){
return firstName + " " + lastName;
}
//最后的结果是
false
[f1 11]
这结果不知道是不是你想要的。
如果不是特别新手的话,而且经常打错代码的话,就弄一个编译环境用吧。省时间,不容易出错
[其他解释]import java.util.Collection; // 1、你没有 import
import java.util.HashSet;
public class BasicContainer {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1", "11"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));// 2、你的Integer都写错了
System.out.println(c.remove(new Name("f1", "11")));
System.out.println(c);
}
}
class Name {
private String firstName, lastName; //3、 你的firstName 都写错了,
//接下来看看还有没有问题,再说···
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String toString() {
return firstName + " " + lastName;
}
}
[其他解释]
import java.util.*;
public class BasicContainer{
public static void main(String[] args){
Collection<Object> c = new HashSet<Object>();
c.add("hello");
c.add(new Name("f1","11"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));//Ingeger
System.out.println(c.remove(new Name("f1","11")));
System.out.println(c);
}
}
class Name{
private String firstName,lastName;//firstName
public Name(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String toString(){
return firstName + " " + lastName;
}
}
[其他解释]我囧,lz你认为哪儿错了?
[其他解释]不知道啊,提示我Collection c = new hashSet();不兼容啊
[其他解释]这个是通过向上转型来创建对象,请问你的子类在哪里?
[其他解释]该回复于2012-11-08 19:53:11被管理员删除
[其他解释]LZ用的是eclipse么? 快捷键: “Shift + /”吧,不用什么都手工敲代码,效率低哈···
[其他解释]代码敲得不规范,另外你之前还得学一下泛型,在集合这块有用
[其他解释]正解!
[其他解释]求教,规范的代码咋写啊
[其他解释]用的JCreator
[其他解释]就是这个结果,3Q
[其他解释]http://bbs.csdn.net/topics/390275427?page=1#post-392867820
[其他解释]
谢谢
[其他解释]import java.util.*;不都包括了那俩包吗
[其他解释]用的JCreator,改正后是这样的
[其他解释]http://bbs.csdn.net/topics/240028892跟这个问题是一样的,import java.util.*不起作用,改成。。。util.Collection,...util.HashSet,就能编译通过
[其他解释]路过,你定义的 Collection c = new HashSet(); 是向父类传子类?
[其他解释]这实在是不懂啊