Java数组
一、基本概述
- 1.数组是在内存中连续存储多个相同数据类型的元素的结构
- 2.数组的长度固定不变,避免数组越界
- 3.数组的元素通过下标访问,一维数组可用一个循环动态初始化,二维
- 数组可用嵌套循环动态初始化,二维数组可以看做一维数组嵌套的构成
- 4.语法:
datatype[] arrayName = new datatype[size];
int[] nums = new int[5];
int[] nums = {1, 2, 4, 7, 12};
//循环输出斐波那契数列的值 //求数列中所有数值的和及平均值 public class Fibonaqi { public static void main(String[] args) { final int N = 20; int[] fibo = new int[N]; //定义一个空的数组,数组长度为20 int sum = 0; //数组和 double avg = 0; //数组的平均值 //1.构造数组 fibo[0] = 0; fibo[1] = 1; for (int i = 2; i < fibo.length; i++) { fibo[i] = fibo[i-1] + fibo[i-2]; } //2.输出数组 for (int i = 0; i < fibo.length; i++) { System.out.print(fibo[i] + " "); sum += fibo[i]; } //3.计算打印和及平均值 avg = sum / fibo.length; System.out.println(); System.out.printf("数组和为:%-5d\t平均值为:%-5.2f",sum,avg); } }
//遍历查找求数组的最大值和最小值 public class MaxMinDemo { public static void main(String[] args) { //1、随机生成一个数组 final int N = 10; int[] nums = new int[N]; for (int i = 0; i < nums.length; i++) { nums[i] = (int) (Math.random()*1000); System.out.print(nums[i]+" "); } //2、遍历比较max min int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int i = 0; i < nums.length; i++) { if(nums[i] > max){ max = nums[i]; } if(nums[i] < min){ min = nums[i]; } } //3、输出最大值 最小值 System.out.println("\n最大值是:"+max+"\t最小值是:"+min); } }
package shuffledemo; public class ShuffleDemo { public static void main(String[] args) { String[] cardscolor = {"黑桃","红心","方块","梅花"}; String[] cardsvalue = {"A","2","3","4","5","6","7", "8","9","10","J","Q","K"}; //每种花色13张牌 final int N = 52; int[] cards = new int[N]; for (int i = 0; i < cards.length; i++) { cards[i] = i; } //洗牌前 System.out.println("洗牌前:"); for (int i = 0; i < cards.length; i++) { System.out.printf("%s-%s",cardscolor[i / 13],cardsvalue[i % 13]); if((i+1) % 13 == 0){ System.out.println(); }else{ System.out.print("\t"); } } System.out.println("-------------------------------------------------------------------------------------"); //换牌 System.out.println("洗牌后:"); for (int i = 0; i < cards.length; i++) { int index =(int)( Math.random() * N); int temp = cards[i]; cards[i] = cards[index]; cards[index] = temp; } //洗牌后 for (int i = 0; i < cards.length; i++) { System.out.printf("%s-%s",cardscolor[cards[i] / 13],cardsvalue[cards[i] % 13]); if((i+1) % 13 == 0){ System.out.println(); }else{ System.out.print("\t"); } } } }
package bubsortdemo; public class BubsortDemo { public static void main(String[] args) { // 冒泡正向对数组进行排序 int[] nums = {67, 49, 66, 46, 22, 9, 90, 11}; //外层循环,它决定一共走几趟 for (int i = 0; i < nums.length-1; i++) { //内层循环,开始逐个比较 //如果我们发现前一个数比后一个数大,则交换 for (int j = 0; j < nums.length - i - 1; j++) { if(nums[j] > nums[j + 1]){ int temp = nums[j]; nums[j] = nums[j + 1]; nums[j +1] = temp; } } } //打印数组 for (int i : nums) { System.out.print(i + ","); } } }
package selectionsort; public class SelectionSort { public static void main(String[] args) { // 使用选择排序法逆向排序 int[] nums = {67, 49, 66, 46, 22, 9, 90, 11}; //内层循环,控制查找的趟数 for (int i = 0; i < nums.length-1; i++) { //每一次将数组的第一个元素和后面所有的数字比较,如果小于则交换 for (int j = i + 1; j < nums.length; j++) { if(nums[i] < nums[j]){ int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } } } //打印数组 for (int i : nums) { System.out.print(i + ","); } } }
import java.util.Arrays; public class SelectionSort { public static void main(String[] args) { // 使用java.util.Arrays.sort()方法进行排序 int[] nums = {67, 49, 66, 46, 22, 9, 90, 11}; Arrays.sort(nums); //打印数组 for (int i : nums) { System.out.print(i + ","); } } }