您的位置:首页 > 其它

【数组】将给定字符串str="1,2,3!4,5,6,0!7,8,9"存入二维数组中。(使用 split(String regex) ) int[][] a

2015-12-13 15:53 281 查看
package com.qianfeng.day_12.homework;

import java.beans.IntrospectionException;

/**

 * 10.将给定字符串str="1,2,3!4,5,6,0!7,8,9"存入二维数组中。(使用 split(String regex) ) int[][] a

 *

 * @author Administrator

 *

 */

public class Test10 {

    public static void main(String[] args) {

            String str="1,2,3!4,5,6,0!7,8,9";

            String[] strs1 = str.split("!");  //切分3节

            

            int[][] a = new int[3][];  //那就确认的数组第一维长度

            //1,2,3

            //4,5,6,0

            //7,8,9

            for(int i = 0; i < strs1.length; i++){

                String[] bb = strs1[i].split(",");  //1  2   3

                a[i] = str2int(bb);

            }

            

            for(int[] a1 : a){

                for(int a2 : a1){

                    System.out.print(a2 + " ");

                }

                System.out.println();

            }

        }

        

        //把String[] --> int[]

        public static int[] str2int(String[] strs){

            int[] aa = new int[strs.length];

            for(int i = 0; i < strs.length; i++){

                aa[i] = Integer.parseInt(strs[i]);

            }

            

            return aa;

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: