运用了静态初始化的方法给数组赋值,同时方法中采用数组类型作为参数传递。 public class Arrayss { public static void main(String[] args) { int[] array={1,2,3}; sum(array); } public static void sum(int[] num){ int s=0; for (int i = 0; i < num.length; i++) { s+=num[i]; } System.out.println(s); } }
这里利用动态初始化给数组赋值,同时采用for-each(增强型for循环)来遍历数组并且输出。
public class Array { public static void main(String[] args) { int[] num=new int[10]; num[0]=3; num[1]=6; num[2]=9; num[3]=8; num[4]=5; num[5]=4; for (int i : num) { System.out.println(i); } } }
这里将方法返回值类型也设立为数组类型,用于将数组里的数倒置输出
public class Revert { public static void main(String[] args) { int[] array={1,2,6,9,7,9,7}; int[] reverse=fan(array); print(reverse); } public static int[] fan(int[] queue){ int[] result=new int[queue.length]; for(int i=0, j=queue.length-1;i<queue.length;i++,j--){ result[i]=queue[j]; } return result; } public static void print(int[] num){ for (int i = 0; i < num.length; i++) System.out.println(num[i]); } }
遍历二维数组并且输出 public class Twoarrays{ public static void main(String[] args) { int[][] num={{0,2},{3,4},{5,6}}; prinf(num); } public static void prinf(int[][] array){ for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) System.out.println(array[i][j]); } } }
二维数组相当于数组里面含有数组,这里用于输出num[0]里面的两个数。
public class Twoarray { public static void main(String[] args) { int[][] num={{0,2},{3,4},{5,6}}; prinf(num[0]); } public static void prinf(int[] array){ for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } }
这是一个冒泡排序过程,方法返回值也是数组类型,还用到了Arrays类的toString方法。方法中设置的flag变量用于检测在冒泡排序以前序列是否已经排好了序,排好了序就break。
import java.util.Arrays; public class Bubblearray { public static void main(String[] args) { int[] array={25,69,89,66,859,66}; int[] numb=bubble(array); System.out.println(Arrays.toString(numb)); } public static int[] bubble(int[] num){ int flag=0; for (int i = 0; i < num.length-1; i++) { int s=0; for (int j = 0; j<num.length-1-i; j++) { if(num[j]>num[j+1]){ s=num[j]; num[j]=num[j+1]; num[j+1]=s; flag=1; } } if(flag==0) break; } return num; } }
用到了Arrays类中的fill(用于给数组赋值),sort(用于给数组从小到大值排序),toString(输出数组)方法
import java.util.Arrays; public class Arratyde { public static void main(String[] args) { int[] num = {1, 69, 6, 5, 45, 89}; Arrays.fill(num,2,4,0); Arrays.sort(num); System.out.println(Arrays.toString(num)); } }
这里输出了一个普通数组,然后将普通数组变为稀疏数组输出,最后再将稀疏数组变为普通数组输出。
import javax.swing.*; import java.util.Arrays; public class SmallArray { public static void main(String[] args) { int[][] num = new int[4][5]; num[2][0] = 1; num[1][0] = 1; for (int[] s : num) { for (int t : s) { System.out.print(t); } System.out.println(); } int s = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { if (num[i][j] == 1) { s++; } } } int[][] num1 = new int[s + 1][3]; num1[0][0] = 4; num1[0][1] = 5; num1[0][2] = s; int count = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { if (num[i][j] == 1) { count++; num1[count][0] = i; num1[count][1] = j; num1[count][2] = num[i][j]; } } } for (int i = 0; i < num1.length; i++) { System.out.println(num1[i][0] + "\t" + num1[i][1] + "\t" + num1[i][2]); } //System.out.println(num1[i][0] + num1[i][1] + um1[i][2]); } 为什么这样就不对 int[][] num2=new int[4][5]; for (int i = 1; i < num1.length; i++) { num2[num1[i][0]][num1[i][1]]=num1[i][2]; } for (int[] u : num2) { for (int t : u) { System.out.print(t); } System.out.println(); } } }