私の以前の質問では、2次元配列の各配列をどのように出力するかを知ったので、以下でこのコードを完成させました。私はこれが1つ/それを行う最も速い方法であるかどうか知りたいですか? if文と入れ子のfor-loops(?)だけではないので、これは線形時間でなければなりません2dアレイを印刷する最も速い方法ですか?
もしそうでないなら、最も効率的な方法は何ですか?
public class Print{
public static void main(String[] args){
int[][] myArray = {{1,2,3,4},{5,6,7,8}};
int i=0;
System.out.print("[");
for(int j=0; j<myArray[0].length; j++){
System.out.print(myArray[i][j]);
if(j != myArray[0].length-1){
System.out.print(", ");
}
if(j == myArray[0].length-1){
System.out.print("], ");
}
}
int k=1;
System.out.print("[");
for(int j=0; j<myArray[1].length; j++){
System.out.print(myArray[k][j]);
if(j != myArray[1].length-1){
System.out.print(", ");
}
if(j == myArray[1].length-1){
System.out.print("]");
}
}
}
}
Output: [1, 2, 3, 4], [5, 6, 7, 8]
編集:私はこれを短くコードする方法を探していません。私はむしろアルゴリズムの速度を最速に探しています。
これは簡単な方法です: 'Arrays.deepToString(myArray)' – shmosel
これはどうしますか?その背後には機能がありますか?私は今それをテストするか分からなかった。 Btw私はそれをコードする短い方法を求めているのではなく、より速いアルゴリズム。 –
http://stackoverflow.com/q/19648240/4821941 – m3kkis