Java Novice Learning Notes--Array (iii): two-dimensional array __java

Source: Internet
Author: User
Tags array definition
definition

1. Two-D array definition
//2. Two-d array of memory space
//3. Irregular array
package me.array;
public class array2demo{public
	
	
	static void Main (string[] args) {
		
		
		//define two-dimensional array
		int[] [] arr={{1,2,3},{4,5,6}};< c8/>//statically initializes
		 
		//prints out two-dimensional array for
		
		(int i=0;i<arr.length;i++) {for
			
			(int j=0;j<arr[i].length;j++) {
					
				System.out.print (arr[i][j]+ "");
				
			}
			Output a column after the carriage return space
			System.out.println ();}}
	


Two-dimensional array memory structure

(picture copyright belongs to "Good Gregor Java Learning notes" all)




Irregular two-dimensional arrays


1. Two-D array definition
//2. Two-d array of memory space
//3. Irregular array
package me.array;
public class array2demo{public
	
	
	static void Main (string[] args) {
		
		//create and print irregular two-dimensional array
		int arr[] [];
		
		Arr=new int[3][];//now indicates that an irregular array
		
		arr[0]=new int[10];//arr[0] points to another array
		arr[1]=new int[3];
		Arr[2]=new int[4];
		
		Assignment for
		(int i=0;i<arr.length;i++) {for
			
			(int j=0;j<arr[i].length;j++) {
				
				arr[i][j]=j}}
			
		}
		
		//output for
		(int i=0;i<arr.length;i++) {for
			
			(int j=0;j<arr[i].length;j++) {
				
				System.out.print (arr[i][j]+ "");
			}
			System.out.println ();
		}
		
		/* Output results: 0 1 2 3 4 5 6 7 8 9 0 1 2 0 1 2 3 
		*///
		
	}
	
}



3. Write a method that returns a double two-dimensional array, which is obtained by parsing string parameters.
such as "1,2;3,4,5;6,7,8"//d[0,0]=1.0 d[0,1]=2.0 d[1,0]=3.0 ... package me.parser; public class teststring{public static void Main (string[] args) {//1. Splitting split (";") with a string
		into three string array//2. Re-decompose split (",")//Declare a two-dimensional array to be used to load the decomposed character String s= "1,2;3,4,5;6,7,8";
		
		The split () method is used to decompose string[] Sfirst=s.split (";");
		
		String[][] Word=new string[sfirst.length][];
		int flag=0;
			for (int i=0;i<sfirst.length;i++) {//Print out separated//system.out.println (Sfirst[i]);
			 /* This statement outputs 1,2 3,4,5 6,7,8 *///next in accordance, separate them into a one-dimensional array string[] Ssecond=sfirst[i].split (",");
			~ System.out.println (ssecond.length);
			~//* Output://~ 2//~---------------------------------//~ 3//~---------------------------------//~ 3 ~---------------------------------//~ *///Description Each Ssencond the length of this one-dimensional array is different word[i]=new string[ssecond.length];//This step determines whether the row is not Each line length of the rule array is assigned to this array for (int j=0;j<ssecond.length;j++) {word[i][j]=SSECOND[J];
		System.out.println ("---------------This is the first" + (i+1) + "secondary cycle-------------------");
		}//Output two-dimensional array System.out.println ("Output two-dimensional array-------------------");
			for (int i=0;i<word.length;i++) {for (int j=0;j<word[i].length;j++) {System.out.print (word[i][j]+ "");
		} System.out.println (); }/* Result:---------------This is the 1th time loop----------------------------------This is the 2nd cycle----------------------------------This is the 3rd cycle -------------------output two-dimensional array-------------------1 2 3 4 5 6 7 8 output two-dimensional array-------------------*///}}



Author: Yanggan
Source: Http://blog.csdn.net/incyanggan
This article is based on the signed 2.5 China mainland license Agreement, Welcome to reprint, deduction or for commercial purposes, but must retain this article's signature Yanggan (including links).


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.