TreeSet problems

finalfantasy0915 2010-02-12 02:20:04
Hi,

I encountered some problems when i am learning the treeSet. The following is the code. Could anyone help me please?

The error messages are "TreetSet cannot be resolved or is not a type". I couldn't tell the problems. Thank you.


======================================
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Demo {
public static void main(String[] args){
HashMap hashMap=new HashMap();

hashMap.put("Schmidt","+49 6227 48597");
hashMap.put("Mayer","+49 6221 1808");
hashMap.put("Schmidt","+49 40 364850");
hashMap.put("Treusch","+49 89 98965456");

Set keys= hashMap.keySet();
Iterator iter=keys.iterator();

while (iter.hasNext()){
String key=(String) iter.next();
String value=(String) hashMap.get(key);
System.out.println("key: "+key+"\tvalue: "+value);
}

TreetSet treetSet=new TreetSet();

treetSet.add(new Customer("Schmidt",48597));
treetSet.add(new Customer("Mayer",180));
treetSet.add(new Customer("Schmidt",3648));

Iterator iter2=treetSet.iterator();
while (iter2.hasNext()){
Customer customer=(Customer)iter2.next();
System.out.println(customer.getName());
}
}

public class Customer implements Comparable{
private String name;
private int id;

public Customer(String name, int id){
this.name=name;
this.id=id;
}

public int getId(){
return id;
}

public String getName(){
return name;
}

public int compareTo(Object otherCustomer){
return this.id=((Customer)otherCustomer).getId();
}
}
}



...全文
86 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobo364 2010-02-15
  • 打赏
  • 举报
回复
netbeans测试通过, tree.add(new Demo().new Customer("Schmidt", 48597));
这个的参数传递的也不对
bobo364 2010-02-15
  • 打赏
  • 举报
回复
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package exp1;

/**
*
* @author 波
*/
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Demo {

public static void main(String[] args) {
HashMap hashMap = new HashMap();

hashMap.put("Schmidt", "+49 6227 48597");
hashMap.put("Mayer", "+49 6221 1808");
hashMap.put("Schmidt", "+49 40 364850");
hashMap.put("Treusch", "+49 89 98965456");

Set keys = hashMap.keySet();
Iterator iter = keys.iterator();

while (iter.hasNext()) {
String key = (String) iter.next();
String value = (String) hashMap.get(key);
System.out.println("key: " + key + "\tvalue: " + value);
}

TreeSet tree = new TreeSet();

tree.add(new Demo().new Customer("Schmidt", 48597));
tree.add(new Demo().new Customer("Mayer", 180));
tree.add(new Demo().new Customer("Schmidt", 3648));

Iterator iter2 = tree.iterator();
while (iter2.hasNext()) {
Customer customer = (Customer) iter2.next();
System.out.println(customer.getName());
}
}

public class Customer implements Comparable {

private String name;
private int id;

public Customer(String name, int id) {
this.name = name;
this.id = id;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public int compareTo(Object otherCustomer) {
return this.id = ((Customer) otherCustomer).getId();
}
}
}
knightzhuwei 2010-02-15
  • 打赏
  • 举报
回复
或者这样晦涩点
import java.util.HashMap; 
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Demo {
public static void main(String[] args){
HashMap hashMap=new HashMap();

hashMap.put("Schmidt","+49 6227 48597");
hashMap.put("Mayer","+49 6221 1808");
hashMap.put("Schmidt","+49 40 364850");
hashMap.put("Treusch","+49 89 98965456");

Set keys= hashMap.keySet();
Iterator iter=keys.iterator();

while (iter.hasNext()){
String key=(String) iter.next();
String value=(String) hashMap.get(key);
System.out.println("key: "+key+"\tvalue: "+value);
}

TreeSet treeSet=new TreeSet();

treeSet.add(new Demo().new Customer("Schmidt",48597));
treeSet.add(new Demo().new Customer("Mayer",180));
treeSet.add(new Demo().new Customer("Schmidt",3648));

Iterator iter2=treeSet.iterator();
while (iter2.hasNext()){
Customer customer=(Customer)iter2.next();
System.out.println(customer.getName());
}
}

class Customer implements Comparable{
private String name;
private int id;

public Customer(String name, int id){
this.name=name;
this.id=id;
}

public int getId(){
return id;
}

public String getName(){
return name;
}

public int compareTo(Object otherCustomer){
return this.id=((Customer)otherCustomer).getId();
}
}
}
knightzhuwei 2010-02-15
  • 打赏
  • 举报
回复
import java.util.HashMap; 
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Demo {
public static void main(String[] args){
HashMap hashMap=new HashMap();

hashMap.put("Schmidt","+49 6227 48597");
hashMap.put("Mayer","+49 6221 1808");
hashMap.put("Schmidt","+49 40 364850");
hashMap.put("Treusch","+49 89 98965456");

Set keys= hashMap.keySet();
Iterator iter=keys.iterator();

while (iter.hasNext()){
String key=(String) iter.next();
String value=(String) hashMap.get(key);
System.out.println("key: "+key+"\tvalue: "+value);
}

TreeSet treeSet=new TreeSet();

treeSet.add(new Customer("Schmidt",48597));
treeSet.add(new Customer("Mayer",180));
treeSet.add(new Customer("Schmidt",3648));

Iterator iter2=treeSet.iterator();
while (iter2.hasNext()){
Customer customer=(Customer)iter2.next();
System.out.println(customer.getName());
}
}


}

class Customer implements Comparable{
private String name;
private int id;

public Customer(String name, int id){
this.name=name;
this.id=id;
}

public int getId(){
return id;
}

public String getName(){
return name;
}

public int compareTo(Object otherCustomer){
return this.id=((Customer)otherCustomer).getId();
}
}
finalfantasy0915 2010-02-15
  • 打赏
  • 举报
回复
Sorry for the silly mistakes. I have been practicing the java by not using the IDE tools to gain a better understanding of this language. I have fixed the errors but still have some errors needed to be fixed.

========================================
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class Demo {
public static void main(String[] args){
HashMap hashMap=new HashMap();

hashMap.put("Schmidt","+49 6227 48597");
hashMap.put("Mayer","+49 6221 1808");
hashMap.put("Schmidt","+49 40 364850");
hashMap.put("Treusch","+49 89 98965456");

Set keys= hashMap.keySet();
Iterator iter=keys.iterator();

while (iter.hasNext()){
String key=(String) iter.next();
String value=(String) hashMap.get(key);
System.out.println("key: "+key+"\tvalue: "+value);
}

TreeSet treeSet=new TreeSet();

treeSet.add(new Customer("Schmidt",48597));
treeSet.add(new Customer("Mayer",180));
treeSet.add(new Customer("Schmidt",3648));

Iterator iter2=treeSet.iterator();
while (iter2.hasNext()){
Customer customer=(Customer)iter2.next();
System.out.println(customer.getName());
}
}

public class Customer implements Comparable{
private String name;
private int id;

public Customer(String name, int id){
this.name=name;
this.id=id;
}

public int getId(){
return id;
}

public String getName(){
return name;
}

public int compareTo(Object otherCustomer){
return this.id=((Customer)otherCustomer).getId();
}
}
}



The error message is "No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo)."
yueguangkai001 2010-02-12
  • 打赏
  • 举报
回复
..................
我发觉了一个问题,好多人还在用手写JAVA代码,没有用过IDE
knightzhuwei 2010-02-12
  • 打赏
  • 举报
回复
"TreetSet cannot be resolved or is not a type".

这么明显的事情 是你拼错了 应该是TreeSet

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧