zoukankan      html  css  js  c++  java
  • Java中二维数组的操作

    //1.二维数组的定义

    //2.二维数组的内存空间

     //3.不规则数组   



     

     输出要放在循环里面,放在外面就报错了

     打debug确定二维数组的关系

     

    现在程序在7与8行进行循环

    ------------------------------------------------------------------------------------------------------------------------------------

    然后经历一个中间阶段

    然后继续

     

     一直到

    就结束了


     

    二维数组内存结构

     

     

     

    不规则二维数组

     
    1.  

     在上面中不能 int  arr=new int[][3],这样会报错,这个的意思就“零行三列”

     

     

     
    1. //3.编写一个方法,返回double型二维数组,数组通过解析字符串参数获得。  
    2. //如"1,2;3,4,5;6,7,8"  
    3. //d[0,0]=1.0 d[0,1]=2.0 d[1,0]=3.0 ....  
    4. package me.parser;  
    5. public class TestString{  
    6.       
    7.     public static void main(String[] args){  
    8.           
    9.         //1.用字符串分解split(";")成三个字符串数组  
    10.         //2.再分解split(",")  
    11.           
    12.         //声明一个二维数组用来装分解好的字符  
    13.           
    14.           
    15.         String s="1,2;3,4,5;6,7,8";  
    16.         //以split()方法 分解  
    17.         String[] sFirst=s.split(";");  
    18.           
    19.         String[][] word=new String[sFirst.length][];  
    20.           
    21.         int flag=0;  
    22.         for(int i=0;i<sFirst.length;i++){  
    23.               
    24.             //打印出已经分开的  
    25.             //System.out.println(sFirst[i]);  
    26.             /*这条语句输出 
    27.             1,2 
    28.             3,4,5 
    29.             6,7,8 
    30.             *///接下来在按照 ,分开他们放入一个一维数组  
    31.               
    32.             String[] sSecond=sFirst[i].split(",");  
    33.              //~ System.out.println(sSecond.length);  
    34.             //~ /*输出:  
    35.             //~ 2  
    36.             //~ ---------------------------------  
    37.             //~ 3  
    38.             //~ ---------------------------------  
    39.             //~ 3  
    40.             //~ ---------------------------------             
    41.             //~ *///说明每次sSencond这个一维数组的长度不同  
    42.             word[i]=new String[sSecond.length];//这步确定行不规则数组的每行长度  
    43.       
    44.             //为这个数组赋值  
    45.             for(int j=0;j<sSecond.length;j++){  
    46.                       
    47.                 word[i][j]=sSecond[j];  
    48.             }  
    49.               
    50.             System.out.println("---------------这是第"+(i+1)+"次循环-------------------");  
    51.         }  
    52.           
    53.         //输出二维数组  
    54.         System.out.println("输出二维数组-------------------");  
    55.         for(int i=0;i<word.length;i++){  
    56.             for(int j=0;j<word[i].length;j++){  
    57.                   
    58.                 System.out.print(word[i][j]+" ");  
    59.             }  
    60.             System.out.println();  
    61.         }  
    62.         /*结果: 
    63.         ---------------这是第1次循环------------------- 
    64.         ---------------这是第2次循环------------------- 
    65.         ---------------这是第3次循环------------------- 
    66.         输出二维数组------------------- 
    67.         1 2  
    68.         3 4 5  
    69.         6 7 8  
    70.         输出二维数组------------------- 
    71.         *///  
    72.     }  
    73. }  
  • 相关阅读:
    2013面试C++小结
    Linux C 面试题总结 .
    [SCOI2011]糖果
    python——简单爬虫
    python——ADSL拨号程序
    python——处理xls表格
    Vsphere初试——架设Panabit行为管理
    Vsphere初试——使用Vsphere client
    Vsphere初试——基本安装
    Python2与Python3的不同点
  • 原文地址:https://www.cnblogs.com/thomasbc/p/6907573.html
Copyright © 2011-2022 走看看