package com.sxt.arrays.test; import java.util.Arrays; /* 1,2,3,4!5,6,7!8,9!12,456,90!32 * 将此字符串以叹号为分割存入二维数组中 * 知识点:字符串+数组 */ public class TestArray { public static void main(String[] args) { String s = "1,2,3,4!5,6,7!8,9!12,456,90!32"; String[] split = s.split("!");//分割 返回字符串数组 System.out.println(Arrays.toString(split)); System.out.println("split.length:"+split.length);//二维数组中一维数组的个数 System.out.println("split[0]:"+split[0]); int [][]arr = new int[split.length][];//split.length:二维数组中一维数组的个数 System.out.println("---------------------------"); for(int i=0; i<split.length; i++){ String[] split2 = split[i].split(",");//每个一维数组内的元素是一个整体 所以以逗号分割 //System.out.println("---->"+Arrays.toString(split2)); arr[i] = new int[split2.length];//初始化一维数组!否则:NullPointerException 动态赋值 for(int j=0; j<arr[i].length; j++){ arr[i][j] = Integer.parseInt(split2[j]); System.out.print(arr[i][j]+" "); } System.out.println(); } // [1,2,3,4, 5,6,7, 8,9, 12,456,90, 32] // split.length:5 // split[0]:1,2,3,4 // --------------------------- // 1 2 3 4 // 5 6 7 // 8 9 // 12 456 90 // 32 } }