日期:2014-05-20  浏览次数:20779 次

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){