• ベストアンサー

二次元配列について

lively-gonの回答

回答No.3

こんにちは > if(str == tbl[i][4]) > { (以下略) String同士の比較では "=" を使うことはできません. equalsやequalsIgnoreCaseを使います. > if(name == tbl[i][4]) のようなint同士だったら比較できるんですけどねぇ. ついうっかりやってしまいそうな感じですね.

関連するQ&A

  • 多次元配列について

    このような課題が出ました。  複数人の名前を入力してから  3教科の点数を全員分入力し、  各教科の平均点と、最高点と取った人の名前を表示せよ。 頑張って途中までやったんですが、ここから先に進めません。 どういうふうにやればいいんでしょうか? import java.io.*; class K203 {  public static void main(String args[]) throws IOException{   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   System.out.print("人数を入力して下さい:");   String str = br.readLine();   int ninzuu = Integer.parseInt(str);   String name[] = new String[ninzuu];   for(int i=0; i<ninzuu; i++){    System.out.print(i+1+"人目の名前を入力してください:");    name[i] = br.readLine();   }   String subject[] = {"国語","数学","英語"};   String tensuu[][] = new String[subject.length][ninzuu];   System.out.println("点数を入力してください");   for(int i=0; i<subject.length; i++){    for(int s=0; s<ninzuu; s++){     System.out.print(name[i]+subject[]);     tensuu[i][s] = br.readLine();    }   }  } }

  • javaの二次元配列について質問です

    配列上にあるただ一つの1を入力に応じて移動させるプログラムを作っています。 たとえば0が入力されたら 0, 0, 0 0, 1, 0 0, 1, 0 → 0, 0, 0 0, 0, 0 0, 0, 0 という風に移動させ、端に行ったら移動できないようにしたいです。 今書いたプログラムだと、最初にある1が残ったままになってしまいます。 int[] p = {-1, -1}; int[][] im = new int[3][3]; Scanner scn = new Scanner(System.in); for (int i = 0; i < p.length; i++) { p[i] = 1; } im[p[0]][p[1]] = 1; for (int i = 0; i < im.length; i++) { for (int j = 0; j < im[i].length; j++) { System.out.print(im[i][j] + ","); } System.out.println(); } int n = 0; n = scn.nextInt(); if (n == 0) { p[0] -= 1; } else if (n == 1) { p[1] += 1; } else if (n == 2) { p[0] += 1; } else if (n == 3) { p[1] -= 1; } im[p[0]][p[1]] = 1; for (int i = 0; i < im.length; i++) { for (int j = 0; j < im[i].length; j++) { System.out.print(im[i][j] + ","); } System.out.println(); }

  • booleanの戻り値について

    配列の等価判定をしたいのですが思うように動かないです。 助けてください import java.io.*; class Kadai4 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int[] TBL1; int[] TBL2; TBL1 = new int[5]; TBL2 = new int[5]; boolean flg=true; System.out.println("配列1を入力"); for (int i = 0; i <= 4; i++) { String s1 = br.readLine(); int t1 = Integer.parseInt(s1); TBL1[i] = t1; } System.out.println("配列2を入力"); for (int i = 0; i <= 4; i++) { String s1 = br.readLine(); int t1 = Integer.parseInt(s1); TBL2[i] = t1; } // 配列表示 System.out.println("配列表示"); for (int i = 0; i < TBL1.length; i++) { System.out.print(" TBL1[" + i + "]=" + TBL1[i]); } System.out.println(""); for (int i = 0; i < TBL2.length; i++) { System.out.print(" TBL2[" + i + "]=" + TBL2[i]); } toka(TBL1, TBL2); System.out.println(""); if (flg == true) { System.out.println("配列は等価"); } else { System.out.println("配列は非等価"); } } private static boolean toka(int[] ss1, int[] ss2) { boolean flg = true; for (int i = 0; i < ss1.length; i++) { if (ss1[i] != ss2[i] || ss1.length != ss2.length) { flg=false } } return flg; } }

  • 多次元配列の処理について

    多次元配列を扱った処理を行ないたいのですが、一部でnullを受け取ってしまい、処理を行うことができません。 引数の配列には次のようなテキストが入っています。 --配列の内容-- 0,名詞-一般,1,5, 0,名詞-数,2,6, 0,名詞-接尾-助数詞,3,7, -------------- この配列をコマンドラインに書き出すと正確に表示されます。 しかし、配列の内容を参照して処理を行なうと2列目の処理のときにnullを参照してしまいます。 プログラムは以下のとおりです。 ---プログラム--- public class Dist {   public static int zairyo(String[][] date) {    int a = 0;    int b = 0;    int c = 0;    int d = 0;    int e = 0;    if(date != null) {     int f = 0;     System.out.println("//Dist//");     for(int m = 0; m < date.length; m++) {      for(int n = 0; n < date[0].length; n++) {       if(date[m][n] != null) {        System.out.print(date[m][n] + "\t");        f++;        if(f == date[0].length) {         System.out.println("");         f = 0;        }       }      }     }     for(int i = 0; i < date.length; i++) {      if(date[i][1] != null) {       if(date[i][1] == "名詞-一般")        a++;       if(date[i][1] == "名詞-数")        b++;       if(date[i][1] == "名詞-接尾-助数詞")        c++;      }else System.out.println("2列目null");     }     int j = date.length - 1;     d = Integer.parseInt(date[j][2]);     e = Integer.parseInt(date[j][3]);    }else System.out.println("Dist:null");    if(a > 0 && b > 0 && c > 0 && d <= 10 && e <= 15) {     return 1;    }else return 0;   } } ------------------------ 原因は何なのでしょうか? ご教授お願いします。

  • CSVファイルを二次元配列に格納するプログラム

    前回のソースコードを改変してみました String Str = ""; int Count = 0; try { File inFile = new File("./res/test.csv"); FileReader fr = new FileReader(inFile); BufferedReader br = new BufferedReader(fr); while (br.ready()) { Str = br.readLine(); String[] temp = Str.split(","); int[] m = new int[temp.length]; for (int i = 0; i < temp.length; i++) { m[i] = Integer.parseInt(temp[i]); System.out.print(m[i] + ","); } System.out.println(); Count++; } br.close(); fr.close(); } catch (Exception e) { System.out.println("ファイル読み込みに関するエラー"); } 今一次元配列になっているのですが、これを二次元配列に代入したいのです Countで何回ループしたかによって二次元配列の縦の要素数としたかったのですが、 そうすると横の要素数になるループ中の一次元配列が使えないので困っています

  • 配列について

      class Array01{ public static void main(String[] args){ int[][] ia = {{11,12},{21},{41,42,44}}; for(int i = 0; i < ia.length; i++){ for(int j = 0; j < ia[i].length; j++ ){ System.out.print(ia[i][j] + " "); } System.out.println(); } } } } (i < ia.length) と (j < ia[i].length)のとこの意味が分かりません。特にiaとia[i]の違いなんか教えてもらえると助かります。for文については理解してるんですが・・・

    • ベストアンサー
    • Java
  • CSVファイルを多次元配列に格納する

    CSVファイルをopenCSVを読み込んでその行と列の要素数の多次元配列を作りその配列にデータを格納したいです。 しかし、データが格納できません。2回目の格納するためにwhileから何かおかしいのではないかと思っています。 なにかわかる方、アドバイスが欲しいです。 public class ReadCSV { public static void main(String[] args){ try{ CSVReader reader = new CSVReader( new FileReader("/home/masa/Desktop/WameiSample.csv")); //配列の宣言 String[] nextLine; //データを配列に入れる要素数を見る int j = 0; nextLine = reader.readNext(); int k = nextLine.length; System.out.println("列数[i]"+k); System.out.println("nextLine"+nextLine); while((nextLine = reader.readNext()) != null){ for (int i=0; i<nextLine.length; i++){ //System.out.print(nextLine[i] + "|" + i + "|"); } //System.out.println(); j++; } System.out.println("行数[j]"+j); //記憶する配列 String[][] Wamei = new String[k][j]; System.out.println("きてるよ"); //データを配列に格納していく int x = 0; while((nextLine = reader.readNext()) != null){ System.out.println("きてるよ");  <---こっから、表示してくれない. for (int y=0; y<nextLine.length; y++){ Wamei[x][y] = nextLine[y]; //多次元配列の要素を表示する System.out.print(Wamei[x][y]+"Wamei"+x+y); } System.out.println(); x++; } } catch (IOException e) { e.printStackTrace(); } } }

    • ベストアンサー
    • Java
  • クラスのフィールドの値を配列に代入する方法

    フィールドの値を代入したいのですが、 java.lang.ArrayIndexOutOfBoundsException というエラーが出てしまい、どうすればいいのかわかりません。 どのように対処すればいいのでしょうか? class Car { String carname; int height; int width; int length; } class public static void main(String[] args) throw IOException { Car car[]; car = new Car[5]; System.out.println("車の情報を入力してください。"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in); for(int i=0; i<5; i++){ car[i] = new Car(); System.out.println(i+1 + "台目の車の名前を入力してください。"); String str1 = br1.readLine(); System.out.println("車の高さを入力してください。"); String str2 = br1.readLine(); System.out.println("車の幅を入力してください。"); String str3 = br1.readLine(); System.out.println("車の長さを入力してください。"); String str4 = br1.readLine(); int h1 = Integer.parseInt(str2); int w1 = Integer.parseInt(str3); int l1 = Integer.parseInt(str4); car[i].carname = str1; car[i].height = h1; car[i].weith =w1; car[i].length =l1; } for(int i=0; i<5; i++){ int c_h[] = new int[5]; c_h[i] = car[i].height; } } よろしくお願いします。

    • ベストアンサー
    • Java
  • 配列のインデックス番号を返したいが

    インデックス番号を返したい x[0]=1 x[1]=2 x[2]=3 x[3]=3 x[4]=5 の場合で探す(返す)値を3とした場合⇒2,3と値が返るようにしたい import java.util.*; class Test7_23 { static int[] arraySrchIdx(int[] a,int x){ int idx = 0; for(int i=0;i<a.length;i++){ //A if(a[i]==x){ a[i] = i; idx++; } else{ a[i] = 0; } } for(int i=0;i<a.length;i++) //B System.out.println(a[i]); int[] b = new int[idx]; for(int i=0;i<a.length;i++){ if(a[i] != 0) for(int j=0;j<idx;j++) b[j]=a[i]; } for(int i=0;i<idx;i++) System.out.println("b["+i+"]="+b[i]); return b; } public static void main(String[]args){ Scanner std = new Scanner(System.in); System.out.print("配列の要素数は:"); int n = std.nextInt(); int[] x = new int[n]; for(int i=0;i<n;i++){ System.out.print("x["+i+"]="); x[i] = std.nextInt(); } System.out.print("探す値:"); int a = std.nextInt(); int[] b = arraySrchIdx(x,a); for(int i=0;i<b.length;i++){ System.out.println(b[i]); } } }

  • コマンドプロンプトでひらがなの入力が出来ません。

    普段グーグル日本語入力を使っています。 OS XPsp2 import java.io.*; class Mojiretuate { public static void main(String[] args) throws IOException { String str = "こんにちは"; String str2; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("文字列当てゲームです。"); System.out.println("答えは何かのあいさつです。"); for(;;){ System.out.println("\n入力"); str2 = br.readLine(); System.out.println("入力文字列の長さ:" + str2.length()); if(str.equals(str2)){ System.out.println("正解です"); break; } else if(str2.length() > str.length()){ System.out.println("長すぎます。"); } else if(str2.length() == str.length()){ System.out.println("長さはあっています。"); } else{ System.out.println("短すぎます。"); } } System.out.println("終了"); } }

    • ベストアンサー
    • Java