650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6D/92/wKioL1VmyZWjiwPwAARpRHKaZ0k137.jpg "title=" array. png "alt=" Wkiol1vmyzwjiwpwaarprhkaz0k137.jpg "/>
Example code one:
Package com;public class arraydemo1 { public static void main (String[] args) { int[] arr = new int[] { 12, 34, 54, 23 }; // int[] arr = {12,34,54,23}; /* system.out.println (Arr[0]); system.out.println (arr[1]); system.out.println (arr[2]); system.out.println (Arr[3]);*/ for (int i = 0; i < arr.length; i++) { system.out.println (Arr[i]); } &nBsp; system.out.println ("---------------------"); boolean[] bs = new boolean[4]; System.out.println (Bs[0]); int[] arrs = new int[4]; for (int i=0;i<arrs.length;i++) { system.out.println (Arrs[i]); } }}
Example code two:
Package com;public class arraydemo2 { public static void main (String[] args) { int[] arr = null; arr[0] = 12; arr[1] = 13; arr[2] = 14; arr[3] = 15; arr[4] = 16; arr[5] = 16; for (int i =0;i<arr.length;i++) { System.out.println ("number=" +arr[i]); } }}
Example code three:
Package com;public class arraydemo3 { public static void main (String[] args) { int[][] arr = new int[3][2]; arr[0][0] = 1; arr[1][0] = 2; ARR[2][0]&NBSP;=&NBSP;3;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ARR[0][1] = 2; arr[1][1] = 2; arr[2][1] = 2; // The two-dimensional array has 3 one-dimensional arrays Each array has default initial values null int[][] arr2 = new int[3][]; arr2[0] = new int[3]; arr2[0][0] = 1; arr2[0][1] = 2; arr2[0][2] = 3; arr2[1] = new int[3]; arr2[1][0] = 4; arr2[1][1] = 5; arr2[1][2] = 6; arr2[2] = new int[3]; arr2[2][0] = 4; arr2[2][1] = 5; arr2[2][2] = 6; int[][] arr3 = {{ 12,12,32},{322,435,65,},{12,324,345}}; for (int x=0;x< arr3.length;x++) { for (int y = 0;y<arr3[x].length;y++) { system.out.println (Arr3[x][y]); } } }}
Example code four:
package com;public class arraydemo4 { /** * Get maximum value * @param arr * @return */ public static int getmax (Int[] arr) { int max = arr[0]; for (int i=0;i<arr.length;i++) { if (Arr[i]>max) { max = arr[i]; } } return max; } /** * Maximum Value * @param arr * @return */ &NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;INT&NBSP;GETMAX2 (Int[] arr) { int max = 0; for (int i=1; i<arr.length;i++) { if (arr[i]>arr[ Max]) { max = i; } } return arr[max]; } /** * Minimum value * @return */ public static int getmin (Int[] arr) { int min = 0; for (int i=0;i< arr.length;i++) { if (Arr[i]<arr[min]) { min = i; } } return arr[min]; } public static void main (String[] args) { //gets the middle maximum and minimum values of the array int[] arr = {4,5,3,1,8,6,7}; int max = arraydemo4.getmax (arr); system.out.println ("Max:" +max); int min = arraydemo4.getmin (arr); &NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Minimum Value:" +min); }}
Example code five:
Package com;public class arraydemo5 { public static void main (String[] args) { //sort the given data int[] arr = {4,5,3,1,8,6,7}; //Select Sort for (int x = 0 ;x <arr.length;x++) { for (int y=x+1 ; y<arr.length;y++) { if (Arr[x]>arr[y]) { int temp = arr[x]; arr[x] = arr[y]; &Nbsp; arr[y] = temp; } } } system.out.println ("After sorting:"); for (int i=0;i<arr.length;i++) { system.out.println (Arr[i]); } }}
Sample Code VI:
Package com;public class arraydemo6 { public static void main (String[] args) { //sort the given data int[] arr = {4,5,3,1,8,6,7}; // 5 4 3 1 8 6 7 // 5 4 3 1 8 6 7 // 5 4 3 8 1 6 7 // 5 4 3 8 6 1 7 // 5 4 3 8 6 7 1 // 5 4 8 6 7 3 1 //Bubbling for (int x=0;x< arr.length;x++) { for (int y = 0;y<arr.length-x-1;y++) {//-x Let each comparison element reduce,-1 avoid array cross-border if (arr[y]<arr[y+1]) { int temp = arr[y]; arr[y] = arr[y+1]; arr[y+1] = temp; } } } system.out.println ("After sorting:"); for (int i=0;i<arr.length;i++) { system.out.println (Arr[i]); } }}
This article is from the "Lao Niu Java" blog, please be sure to keep this source http://liuyj.blog.51cto.com/2340749/1655996
An array of javase