我有个数组如下:
String[][] arrA = {(0,20},(1,21),(2,22),(3,22),(4,23),(5,20),(6,23),(7,24),(8,20)...};想按每隔3个取出来一维中数值,其余一维为空,二维中所有数保留,结果赋值给另一个数组:arrB = {(0,20),("",21),("",22),("",22),(4,23),("",20),("",20),("",23),(7,24),("",20),...};这个怎么写啊?

解决方案 »

  1.   

    arrB = {(0,20),("",21),("",22),("",22),(4,23),("",20),("",20),("",23),(7,24),("",20),...}; 这个好像不符合前面楼主说的每隔3个取出一个数
      

  2.   


    public static void main(String[] args) {
    String[][] arr = { { "0", "20" }, { "1", "21" }, { "2", "22" },
    { "3", "22" }, { "4", "23" }, { "5", "20" }, { "6", "23" },
    { "7", "24" }, { "8", "20" } };
    for (int i = 1; i < arr.length; i++) {
    for (int j = 0; j < 2; j++) {
    if (i % 4 != 0) {
    arr[i][0] = " ";
    }
    }
    } for (int i = 0; i < arr.length; i++) {
    System.out.print(arr[i][0] + ",");
    System.out.print(arr[i][1]);
    System.out.println();
    }
    }
      

  3.   

    public class Test { /**
     * @param args
     */
    public static void main(String[] args) {

    String[][] str = {{"0", "20"},{"1", "21"},{"2", "22"},{"3", "23"},{"4", "24"},{"5", "25"},{"6", "26"},
    {"7", "27"},{"8", "28"},{"9", "29"},{"10", "30"},{"11", "31"},};
    String[][] strB = str;

    for(int i=0; i<strB.length; i++) {
    for(int j=0; j<strB[i].length; j++) {
    System.out.print(strB[i][j] + "\t");
    }
    System.out.println();
    }

    int count = 4;
    for(int i=0; i<str.length; i++, count++) {
    if(count == 4){
    count = 0;
    } else {
    str[i][0] = "";
    }
    }
    System.out.println("-----------------");
    for(int i=0; i<strB.length; i++) {
    for(int j=0; j<strB[i].length; j++) {
    System.out.print(strB[i][j] + "\t");
    }
    System.out.println();
    }
    }}
      

  4.   


    public class Test { /**
     * @param args
     */
    public static void main(String[] args) {

    String[][] str = {{"0", "20"},{"1", "21"},{"2", "22"},{"3", "23"},{"4", "24"},{"5", "25"},{"6", "26"},
    {"7", "27"},{"8", "28"},{"9", "29"},{"10", "30"},{"11", "31"},};
    String[][] strB = str;

    for(int i=0; i<strB.length; i++) {
    for(int j=0; j<strB[i].length; j++) {
    System.out.print(strB[i][j] + "\t");
    }
    System.out.println();
    }

    int count = 4;
    for(int i=0; i<str.length; i++, count++) {
    if(count == 4){
    count = 0;
    } else {
    str[i][0] = "";
    }
    }
    System.out.println("-----------------");
    for(int i=0; i<strB.length; i++) {
    for(int j=0; j<strB[i].length; j++) {
    System.out.print(strB[i][j] + "\t");
    }
    System.out.println();
    }
    }}
      

  5.   


    public class Test6 {
    public static void main(String args[]){
    String[][] arrA = {{"0","20"},{"1","21"},{"2","22"},{"3","22"},{"4","23"},{"5","20"},{"6","23"},{"7","24"},{"8","20"}}; 
    String[][] arrB = arrA;
    int count = 0;
    for(int i = 0;i < arrB.length; i++){
    if(count % 4 != 0)
    arrB[i][0] = " ";
    count++;
    }
    for(int i = 0; i < arrB.length; i++){
    for(int j = 0; j < arrB[i].length; j++)
    System.out.print(" "+ arrB[i][j]);
    System.out.println();
    }
    }
    }
      

  6.   

    留底了啊,不是一个arrA,一个arrB吗
      

  7.   

    刚才试了一下,String数组好像是引用传递…………
      

  8.   


    你那个arrB指向arrA,把arrA操作掉了…………
      

  9.   

    恩,确实,如果要生成一个新的数组不大方便,要保留以前的那就应该采用集合类,如HashMap,一开始不拷贝,直接生成一个新的hashmap,把改后的直接插入到新的hashmap中