JAva配列のいくつかの作成方法
1708 ワード
1.一次元配列
2.2 D配列
package array;
import static org.junit.Assert.*;
import org.junit.Test;
public class Array1 {
@Test
public void array1() {
/* */
/*1. 5 */
int[] a1=new int[5];
/*2. , */
int[] a2=new int[]{1,2,3,4,5,6};
/*3. */
int[] a3={1,2,3,4,5,6};
System.out.println("a1 :"+a1.length);
for(int i=0;i<a2.length;i++){
System.out.println("a2="+a2[i]);
}
for(int j=0;j<a3.length;j++){
System.out.println("a3="+a3[j]);
}
}
}
2.2 D配列
package array;
import static org.junit.Assert.*;
import org.junit.Test;
public class Array2 {
@Test
public void test() {
/* */
int[][] a1=new int[3][2];
a1[0][0]=1;
a1[0][1]=2;
a1[1][0]=3;
a1[1][1]=4;
a1[2][0]=5;
a1[2][1]=6;
/* */
String[][] a2={{"q","w"},{"e","r"},{"t","y"}};
System.out.println(" ");
for(int i=0;i<a1.length;i++){
for(int j=0;j<a1[i].length;j++){
System.out.print(a1[i][j]+" ");
}
System.out.println();
}
System.out.println(" ");
for(int i=0;i<a2.length;i++){
for(int j=0;j<a2[i].length;j++){
System.out.print(a2[i][j]+" ");
}
System.out.println();
}
}
}