2017-11-21 14 views
0

私はこれまでのところ、私のコードはこれで、列によってString [][]に文字の数をカウントする:String [] []内の各列の文字数を数えるには?

for(int j = 0 ; j<matrix[0].length ;j++){ 
     for(int i = 0 ; i< matrix.length ;i++) 
     if (Character.isLetter(matrix[j][i].charAt(j))) 
     countChar++; 
     } 
     System.out.println(countChar + "letters"); 
     return countChar; 

が、プログラムの出力が文字列の場合、文字列は、例えば を持っているどのように多くの要素を数えます:

String [][] C = { 
    {"abc", "abcd" ,  "abcd"}, 
    {"oroeo", "kakakak" ,  "alsksjk"}, 
    {"abcdef", "asdasdasdasd", "asdasdasdasd"}, 
}; 

結果は9ですが、すべてのヘルプが大幅にapreciatedさ ありがとう14(列で文字の数)でなければなりません!

+0

列の文字数を数えますか? –

+0

あなたは何を意味するのでしょうか?例を挙げてください。あなたは例を挙げましたが、どのように数えるべきか説明してください。 –

+0

申し訳ありませんが、私は以下の私の答えはあなたが現在受け入れているものよりはるかに優れていると思います。 –

答えて

0

2次元行列は、行の配列または列の配列として定義できます。私はあなたが行の配列として定義していると仮定している今、特定の列の値を取得したい。

だからあなたのデータは次のようになります。

abc    abcd    abcd 
oroeo   kakakak   alsksjk 
abcdef   asdasdasdasd  asdasdasdasd 

3行3列。

真ん中の列の値(インデックス1)は、例えば取得するあなたは配列の要素を取得する必要があります:私はあなたがすべての値の長さの合計をカウントしようとしていると思います

matrix[0][1] 
matrix[1][1] 
matrix[2][1] 

を各列にそれは次のように行くだろう:

// assume that the matrix has at least one row and thas the same number of columns in each row 
    // take the number of columns in the first row for reference 
    int numberOfColumns = matrix[0].length; 
    for(int col = 0; col < numberOfColumns; col++) { 
     int count = 0; 
     // iterate over all the rows 
     for(String[] row : matrix) { 
      // count the length of the element in position col of this row 
      count += row[col].length(); 
     } 
     System.out.printf("%s characters in column %s", count, col); 
    } 
+0

あなたは正しいです、私はあなたの答えがそれについて残念を見ていない!はい、それは各列を投げて、それぞれの文字を数えるので、より良い選択です! – TheVonWaff

0
int n = 0; 
// iterate row by row 
for (int i = 0; i < C.length; i++) { 
    n += C[i][0].length(); 
    // get the string at index 0 (or 1 or 2.. whichever you want) of the array and append its length 
    // if you expect the string to contain numbers, then 
    // run a for-loop on the string and check if its a letter 
} 
System.out.println(n); 
0

問題以下試しがあなたのために無forループです。反復は、あなたの行列の大きさに制限されているの:

for(int i = 0 ; i<C[0].length ;i++) { 
       String matrixElement = C[i][0]; 
       System.out.println(matrixElement); 
       for(int k =0 ;k < matrixElement.length();k++) 
       if (Character.isLetter(matrixElement.charAt(k))) 
        countChar++; 
     } 
+0

あなたの提案が私の問題を解決してくれてありがとう! – TheVonWaff

+0

ありがとうございました。 – Ravik

0

してください、フォーマットあなたのコード実行やループをブラッシュアップ:

private static int countByColumn(String[][] matrix, int column) { 
    if (column < 0) 
     return 0; // Or throw exception 

    int countChar = 0; 

    for (String[] line : matrix) { 
     //DONE: jagged array: it may appear that the line is too short 
     if (line.length <= column) 
     continue; 

     String item = line[column]; 

     for (int i = 0; i < item.length; ++i) 
     if (Character.isLetter(item.charAt(i))) 
      countChar += 1; 
    } 

    return countChar; 
    } 

テスト:あなたがしようとしている

// 14 
    int test = countByColumn(C, 0); 
関連する問題