2017-01-20 9 views
-1

私はプログラムに書いたことがあり、配列中で最も長い文字列を見つけました。今私は追加の機能が必要です。これは 配列の中で最も長い文字列(複製中)

  • 最長
  • なければならない

    1. :N列の最大文字数は、例えば入力のM列

      に等しい場合には、出力にだけ複製列を含みますアレイ内

    2. ストリング

    Outpu tは次のようになります。

    配列
    1. 最長それらの両方(11文字)が最長の文字列(だけではなく、最初にそれらのが、両方!)ですので。前もって感謝します!以下

      コード:

      public static void main(String[] args) throws Exception 
           { 
          ArrayList<String> list = new ArrayList<String>(); 
          ArrayList <Integer> numList = new ArrayList<Integer>(); 
      
          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
      
          for(int i=0; i<5; i++) { 
           String s = reader.readLine(); 
           list.add(i, s); 
           numList.add(i, s.length()); 
          } 
      
          int maxN = numList.get(0); 
          String maxS = list.get(0); 
      
          for(int i=0; i<list.size(); i++) { 
      
           if(numList.get(i)>maxN) { 
            maxN=numList.get(i); 
            maxS = list.get(i); 
           } 
          } 
          System.out.println(maxS); 
      } 
      
    +4

    何を質問? –

    +0

    あなたは最初の部分を書いているので、続ける – CraigR8806

    +0

    そして何をすべきかわからない;( – Rus9Mus9

    答えて

    1

    あなたはあなたの結果が含まれているし、エントリを反復します別のリストを追加することができます。

    ArrayList<String> result = new ArrayList<>(); 
    for(int i=0;i<list.size();i++){ 
        if(list.get(i).length()==maxS){ 
         result.add(list.get(i)); 
         System.out.println(list.get(i)); 
        } 
    } 
    
    0

    あなたは厳密その後、互いに配列要素の文字列の長さを比較し、あなたが最初のリストのArrayListを反復として遭遇する長さに対処するつもりなら。元のリスト ArrayListに含まれているすべての要素の文字列の長さを保持するために、まだ別の整数ArrayListを作成する必要はありません。反復が実行されている間は、どのような条件を設定するのかはすべて問題です。

    @Efkaは既に指摘しているように、長さが同じで同じ長さの実際の要素文字列を保持し、それらの結果を表示する別のString ArrayListを持つことができます。

    それで自然にあなたが成功したので、文字列の要素を持つリスト ArrayListを満たしていることを想定:

    ArrayList <String> dupList = new ArrayList<>(); // Will hold the results 
    
    // Need to start somewhere so... 
    // Initialize maxN with the string length of the first 
    // element contained within the 'list' ArrayList. 
    int maxN = list.get(0).length(); 
    
    // Add the first element to the duplicate list. 
    dupList.add(list.get(0)); 
    
    // We start our iteration from 1 instead of 0 
    // because we've already processed index 0 
    // above to establish a starting point. 
    for(int i = 1; i < list.size(); i++) { 
        int length = list.get(i).length(); //get the element length 
        if(length >= maxN) { 
         if (length > maxN) { 
          // Clear the duplicate list since there is 
          // now an elemental string that is longer. 
          dupList.clear(); 
          // Set maxN to the new longest length 
          maxN = list.get(i).length(); 
         } 
         // Add the new longest or equal to the longest 
         // element encountered to the duplicate list. 
         dupList.add(list.get(i)); 
        } 
    } 
    

    が決定した結果を表示するには、あなたがこのようsomethngを使用することができます:

    // Display the determined results... 
    for (int i = 0; i < dupList.size(); i++) { 
        System.out.println(dupList.get(i)); 
    } 
    
    関連する問題