标签:wap auto arrays 显示 style soft png span mon
1、数组是什么
2、一个实例:遍历显示12月都有多少天
public class Example3 { public static void main(String[] args) { int[] month = new int[]{31,28,31,30,31,30,31,31,30,30,30,31}; for(int i = 0 ; i < 12; i++){ System.out.println((i+1)+"月份的天数是"+month[i]+"天"); } } }
3、数组操作==遍历
public class Trap { public static void main(String[] args) { int b[][] = new int[][]{{0},{1,2,3},{4,5,6}}; for(int i = 0 ; i < b.length ; i++){ for(int k = 0 ; k < b[i].length ; k++){ System.out.print(b[i][k]); } System.out.println(); } } }
4、数组操作==填充和替换
实例1:
import java.util.Arrays; public class Swap { public static void main(String[] args) { int[] a = new int[5]; Arrays.fill(a,8); for(int i = 0 ; i < a.length ; i++){ System.out.println("第"+i+"个元素是" + a[i]); } } }
实例2:
import java.util.Arrays; public class Displace { public static void main(String[] args) { int arr[] = new int[]{45,12,25,23}; Arrays.fill(arr,1,2,3); for(int i = 0 ; i < arr.length ; i++){ System.out.println("第"+i+"个元素是" + arr[i]); } } }
5、数组操作==排序
import java.util.Arrays; public class Taxis { public static void main(String[] args) { int arr[] = new int[]{15,8,2,23,36}; Arrays.sort(arr); for(int i = 0 ; i < arr.length ; i++){ System.out.println("第"+i+"个元素是" + arr[i]); } } }
6、数组操作==复制
实例1:
import java.util.Arrays; public class Copy { public static void main(String[] args) { int arr[] = new int[]{15,8,2,236}; int newarr[] = Arrays.copyOf(arr,5); for(int i = 0 ; i < newarr.length ; i++){ System.out.println("第"+i+"个元素是" + newarr[i]); } } }
实例2:
import java.util.Arrays; public class repeact { public static void main(String[] args) { int arr[] = new int[]{15,8,2,236}; int newarr[] = Arrays.copyOfRange(arr,0,3); for(int i = 0 ; i < newarr.length ; i++){ System.out.println("第"+i+"个元素是" + newarr[i]); } } }
7、数组操作==数组查询
实例1:
import java.util.Arrays; public class Example4 { public static void main(String[] args) { int arr[] = new int[]{15,8,2,236}; Arrays.sort(arr); int index = Arrays.binarySearch(arr,15); System.out.println("2的索引位置是:"+index); } }
实例2:
import java.util.Arrays; public class Rakel { public static void main(String[] args) { String str[] = new String[]{"b","ff","cccc","aa"}; Arrays.sort(str); int index = Arrays.binarySearch(str,0,2,"b"); System.out.println("ff的索引位置是:"+index); } }
8、
9、
标签:wap auto arrays 显示 style soft png span mon
原文地址:https://www.cnblogs.com/pangchunlei/p/12247788.html