Operation of a two-dimensional array in Java

Source: Internet
Author: User

1. Definition of two-D arrays

2. Memory space for two-d arrays

//3. Irregular arrays



The output should be placed inside the loop, and the error will be on the outside.

Hit debug to determine the relationship between two-dimensional arrays

Now the program loops between 7 and 8 lines

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

And then go through an intermediate stage

And then continue

One until

It's over.


Two-dimensional array memory structure

Irregular two-dimensional arrays
  1. 1. Definition of two-D arrays
  2. 2. Memory space for two-d arrays
  3. 3. Irregular arrays
  4. Package Me.array;
  5. Public class array2demo{
  6. public static void Main (string[] args) {
  7. //Create and print irregular two-dimensional arrays
  8. int arr[] [];
  9. arr=New int[3][]; Now the description is an irregular array
  10. arr[0]=new int[]; Arr[0] point to another array
  11. arr[1]=new int[3];
  12. arr[2]=new int[4];
  13. //Assigned value
  14. For (int i=0;i<arr.length;i++) {
  15. For (int j=0;j<arr[i].length;j++) {
  16. Arr[i][j]=j;
  17. }
  18. }
  19. //Output
  20. For (int i=0;i<arr.length;i++) {
  21. For (int j=0;j<arr[i].length;j++) {
  22. System.out.print (arr[i][j]+"");
  23. }
  24. System.out.println ();
  25. }
  26. /* Output Result:
  27. 0 1 2 3 4 5 6 7 8 9
  28. 0 1 2
  29. 0 1 2 3
  30. *///
  31. }
  32. }

[Java]View PlainCopy print?
  1. 3. Write a method that returns a double-type two-dimensional array, which is obtained by parsing the string argument.
  2. such as "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. public static void Main (string[] args) {
  7. //1. Splitting the split with a string (";") into three array of strings
  8. //2. Splitting Split (",")
  9. //Declare a two-dimensional array to fit the decomposed characters
  10. String s="1,2;3,4,5;6,7,8";
  11. //Split () method to decompose
  12. String[] Sfirst=s.split (";");
  13. String[][] word=new string[sfirst.length][];
  14. int flag=0;
  15. For (int i=0;i<sfirst.length;i++) {
  16. //Print out the separated
  17. //system.out.println (Sfirst[i]);
  18. / * This statement outputs
  19. The
  20. 3,4,5
  21. 6,7,8
  22. *///next in accordance, separate them into a one-dimensional array
  23. String[] Ssecond=sfirst[i].split (",");
  24. //~ System.out.println (ssecond.length);
  25. //~/* Output:
  26. //~ 2
  27. //~ ---------------------------------  
  28. //~ 3
  29. //~ ---------------------------------  
  30. //~ 3
  31. //~ ---------------------------------             
  32. //~ *///The length of the one-dimensional array is different each time Ssencond
  33. word[i]=New String[ssecond.length]; This step determines the length of rows for each row of an irregular array
  34. //Assign a value to this array
  35. For (int j=0;j<ssecond.length;j++) {
  36. WORD[I][J]=SSECOND[J];
  37. }
  38. System.out.println ("---------------This is the first" + (i+1) +"secondary cycle-------------------");
  39. }
  40. //Output two-dimensional array
  41. System.out.println ("Output two-dimensional array-------------------");
  42. For (int i=0;i<word.length;i++) {
  43. For (int j=0;j<word[i].length;j++) {
  44. System.out.print (word[i][j]+"");
  45. }
  46. System.out.println ();
  47. }
  48. /* Results:
  49. ---------------This is the 1th cycle-------------------
  50. ---------------This is the 2nd cycle-------------------
  51. ---------------This is the 3rd cycle-------------------
  52. Output two-dimensional array-------------------
  53. 1 2
  54. 3 4 5
  55. 6 7 8
  56. Output two-dimensional array-------------------
  57. *///
  58. }
  59. }

Operation of a two-dimensional array in Java

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.