2014-09-06 54 views
-2

我在做一个项目,在Java中的大学,我有一个这样的输入:我的第一个Java项目

i 4   //that are the iteration 

a nissan car motor -1  // "a" means add operation "nissan" is the element and "car e motor" 
             // are the tags associated to nissan 

a apple fruit iphone macbook -1 

a peach fruit color -1 

s fr -1    // "s" is the subtag that I have to find in the tag 

i 3 

a soccer ball player -1 

a volley ball shoes -1 

s bal -1 

END    // "END" is the end of input 

输出:

2 

2 

因为第一次迭代在它找到两个元素的标签以“fr”开头, ,在第二次迭代中它找到两个标签以“bal”开头的元素。

class main 
{ 
    private static boolean trovato; 

    public static void main (String args []) throws IOException 
    { 
     HashMap<String, HashSet<String>> map = new HashMap <String,HashSet<String>>(); 
     BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 
     HashSet<String> totale = new HashSet <String>() ; 

     String line ; 
     String numiter; 
     Integer numiter2 = 0 ; 
     String[] parts = null ; 

     HashSet<String> hs = new HashSet<String> () ; 
     HashSet<String> tmp = new HashSet<String> () ; 
     Vector <Integer> conti = new Vector<Integer>(); 

     boolean entrato = false ; 

     line = stdin.readLine(); 

     while (!line.equals("<END>")) 
     { 
      if (line.startsWith("i")) 
      { 
       map.clear(); 
       hs.clear(); 
       totale.clear(); 
      } 

      parts = line.split("[\\s\\-1]");  

      if (parts[0].equals("a"))    
      { 

       hs.add(parts[1]); 

       for (int r = 2 ; r < parts.length ; r++)  
       { 

        map.put(parts[r], hs);            
       } 

      } 


      System.out.println("totale" + totale.size()); 
      if (parts[0].equals("s")) 
      { 
       entrato = false ; 
       for (String key : map.keySet())         
       { 
        if (key.startsWith(parts[1]))         
        { 
         entrato = true ;    
         totale.addAll(map.get(key));         
        } 
       } 
       System.out.println(totale.size()); 
      } 
      line = stdin.readLine(); 
     } 
    } 
} 

该代码有效,但我的老师想要一个更有效的方式来做到这一点。我不知道该怎么做。提前感谢。

+0

帮帮我!我应该使用SortedMap吗?TreeMap。哪个更适合我的问题? – user3572461 2014-09-06 12:52:21

回答

0

以供将来参考尝试保持您的问题比这更具体。调试是你的工作;我建议在来这里之前回顾课堂上教授的概念。我们更愿意回答一个比这更具体的问题。如果您需要进一步的帮助,请随时与我联系。

+0

我不会很好地写英文。对不起,所以你不明白我的问题? – user3572461 2014-09-06 16:06:18

+0

你只是向我们扔了一堆代码。这个问题不是很具体,所以我真的不明白你在做什么 – spb1994 2014-09-06 20:07:33

相关问题