Java input Two-dimensional array of any length

Source: Internet
Author: User

Two-dimensional array, each dimension you want to enter any number of characters separated by a space can use the following code, if there is a better way to welcome the discussion.

Package basic_practice;

Import Java.util.Scanner;

public class Main {public
	static void Main (string[] args) {
		int a[][]=new int[100][100];
		int n;
		Scanner in=new Scanner (system.in);
		N=in.nextint ();
		Skip the line newline character  
		in.nextline ();	
		for (int i=0;i<n;i++) {
		String strLine = In.nextline ();   
		Scanner s = new Scanner (strLine); 
		int j=0;
		while (S.hasnextint ()) {
			a[i][j++]=s.nextint ();		
				}
		}	
		Output for
		(int i=0;i<n;i++) {
			int j=0;
			while (a[i][j]!=0)
			System.out.print (a[i][j++]+ "  ");
			System.out.println ();}}




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.