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

来源:互联网 发布:消防知识知多少手抄报 编辑:程序博客网 时间:2024/04/28 11:31
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;
        }
    }

0 0
原创粉丝点击