一维和多维数组

数组

同一种类型数据的集合。其实数组就是一个容器。

数组的初始化

	[] 数组名 = new 元素类型[元素个数或数组长度];
		int[] arr3=new int[3];
		arr3[0]=1;
		arr3[1]=5;
		arr3[2]=6;

或:

	 [] 数组名 = new 元素类型[]{元素,元素,……};
示例:int[] arr = new int[]{3,5,1,7};// 数组初始化时不能指定数组长度值。
	 int[] arr = {3,5,1,7}//此时自动确定a.length值为4
  • 创建数组后,开始有批量数据(10个整数,默认为0)。
  • new一个数组(创建一个数组),实际上是返回一个引用(内存地址),将该引用赋给数组名arr,则数组名arr指向该数组并全权代表该数组。
  • arr可以使用很多属性和方法(如length属性)。

常用算法

  1. 清零

    for(i = 0; i < 10; i++)
        a[i] = 0;
    
  2. 输入

    for(i = 0; i < a.length; i++)
        a[i] = sc.nextInt();
    
  3. 输出

    for(i=0;i<a.length;i++)
        System.out.println(a[i]);
    

    System.out.println(Arrays.toString(a));
    
    
  4. 复制

    int[] arr2=java.util.Arrays.copyOf(原数组名,新数组长度);
    

    一维数组实例

    1. 一维数组基本操作。

      • 输入十个数组并求其最大值。
                int[] a = new int[10];
                int i;
                int max;
                Scanner sc = new Scanner(System.in);
                for (i = 0; i < a.length; i++) {
                    System.out.println("请输入第" + i + "组数");
                    a[i] = sc.nextInt();
                }
                System.out.print("数组元素分别是:");
                for (i = 0; i < a.length; i++) {
                    System.out.print(a[i] + "  ");
                }
                max = a[0];
                for (i = 1; i < a.length; i++) {
                    if (a[i] > max) {
                        max = a[i];
                    }
                }
                System.out.println("其中最大值为:" + max);
        
      • 输入10个数组并求其最小值。
                int[] a = new int[10];
                int i;
                int min;
                Scanner sc = new Scanner(System.in);
                for (i = 0; i < a.length; i++) {
                    System.out.println("请输入第" + i + "组数");
                    a[i] = sc.nextInt();
                }
                System.out.print("数组元素分别是:");
                for (i = 0; i < a.length; i++) {
                    System.out.print(a[i] + "  ");
                }
                min = a[0];
                for (i = 1; i < a.length; i++) {
                    if (a[i] < min) {
                        min = a[i];
                    }
                }
                System.out.println("其中最大值为:" + min);
        
      • 随机产生10个100以内并求其最大值的下标。
                int[] a = new int[10];
                int i;
                int maxIndex;
                Random rd = new Random();
                for (i = 0; i < a.length; i++) {
                    a[i] = rd.nextInt(100);
                }
                maxIndex = 0;
                for (i = 1; i < a.length; i++) {
                    if (a[i] > a[maxIndex]) {
                        maxIndex = i;
                    }
                }
                System.out.println("输入的数组分别为:");
                for (i = 1; i < a.length; i++) {
                    System.out.println(a[i]);
                }
                System.out.println("其中最大值为:" + a[maxIndex]);
                System.out.println("最大值的下标为:" + maxIndex);
        
      • 随机产生10个100以内的数组并求其平均值。
                int[] a = new int[10];
                int sum = 0;
                int i;
                int average;
                System.out.println("随机产生10个数组");
                for (i = 0; i < a.length; i++) {
                    a[i] = (int) (Math.random() * 100);
                }
                for (i = 0; i < a.length; i++) {
                    sum += a[i];
                }
                average = sum / 10;
                for (i = 0; i < a.length; i++) {
                    System.out.println(a[i] + "   ");
                }//用System.out.println(Arrays.toString(a));也可
                System.out.println("平均值为" + average);
        
      • 逆置一维数组。
                int i, t;
                int[] a = new int[10];
                for (i = 0; i < a.length; i++) a[i] = (int) (Math.random() * 100);
                System.out.println(Arrays.toString(a));
                for (i = 0; i < a.length / 2; i++) {
                    t = a[i];
                    a[i] = a[a.length - i - 1];
                    a[a.length - i - 1] = t;
                }
                System.out.println(Arrays.toString(a));
        
    2. 利用一维数组计算某年某月有几天。

              int year, month;
              Scanner sc = new Scanner(System.in);
              int[] a = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
              System.out.println("请输入年份");
              year = sc.nextInt();
              do {
                  System.out.println("请输入月份[1~12]");
                  month = sc.nextInt();
              } while (month > 12 || month < 1);
              if (month == 2) {
                  if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) a[month]++;
              }
              System.out.print(year + "年" + month + "月,有" + a[month] + "天");
      
      • 求今天是今年的第几天。
                int year, month, day;
                int count = 0;
                int[] a = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                Calendar cal = Calendar.getInstance();
                year = cal.get(Calendar.YEAR);
                month = cal.get(Calendar.MONTH) + 1;
                day = cal.get(Calendar.DATE);
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    a[2]++;
                }
                for (int i = 1; i < month; i++) count += a[i];
                count += day;
                System.out.println("今天是" + year + "年," + month + "月" + day + "日");
                System.out.println("是今年第" + count + "天");
        
      • 计算自己从出生到今天的天数
        import java.util.Scanner;
        import java.util.Calendar;
        public class Report {
            public static void main(String[]args) {
                int[] a = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                int year, month, day;
                int yearToday, monthToday, dayToday;
                int count = 0;
                Calendar cal = Calendar.getInstance();
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入你的生日(年、月、日空格间隔)");
                int yearBirth = year = sc.nextInt();
                int monthBirth = month = sc.nextInt();
                int dayBirth = day = sc.nextInt();
                yearToday = cal.get(Calendar.YEAR);
                monthToday = cal.get(Calendar.MONTH) + 1;
                dayToday = cal.get(Calendar.DATE);
        
                if (year % 4 == 0 && year % 100 != 100 || year == 400) a[2] = 29;
                count += a[month] - day + 1;
                for (month++; month < 13; month++) {
                    count += a[month];
                }
                a[2] = 28;
                // 计算出生那年剩余的天数
                for (year++; year < yearToday; year++) {
                    if (year % 4 == 0 && year % 100 != 100 || year == 400) a[2] = 29;
                    count += 337 + a[2];
                    a[2] = 28;
                }
                // 计算出生那年到今年
                if (yearToday % 4 == 0 && yearToday % 100 != 100 || yearToday == 400) a[2] = 29;
                else a[2] = 28;
                for (month = 1; month < monthToday; month++) count += a[month];
                // 计算今年到今天的天数
                count += dayToday;
                System.out.println("生日是:" + yearBirth + "年" + monthBirth + "月" + dayBirth + "日");
                System.out.println("今天是:" + yearToday + "年" + monthToday + "月" + dayToday + "日");
                System.out.println("总共生活了" + count + "天");
            }
        }
        
    3. 将一个整数转换为二进制并输出。

              int[] arr = new int[10];
              int num, i, j;
              Random rd = new Random();
      
              System.out.print("随机生成了一个1000以内的整数:");
              System.out.println(num = rd.nextInt(1000));
      
              i = 0;
              while(num > 0) {
                  arr[i++] = num % 2;
                  num /= 2;
              }
      
              System.out.println("对应的二进制是:");
              for(j = i - 1; j >= 0; j--) {
                  System.out.print(arr[j]);
              }
              System.out.println();
      
    4. 统计各分数段人数、及格率、平均分、最高最低分。

      import java.util.*;
      public class Report {
          public static void main(String[]args) {
              int[] score = new int[45];
              int[] count = new int[5];
              int i, sum = 0;
              double average, rate;
              System.out.println("全班同学成绩如下:");
              for (i = 0; i < score.length; i++) {
                  score[i] = (int) (Math.random() * 100);
                  if ((i + 1) % 9 == 0) System.out.println(score[i]);
                  else System.out.print(score[i] + " ");
              }
              Arrays.sort(score);
              for (i = 0; i < score.length; i++) {
                  sum += score[i];
                  if (score[i] > 89) count[0]++;
                  else if (score[i] > 79) count[1]++;
                  else if (score[i] > 69) count[2]++;
                  else if (score[i] > 59) count[3]++;
                  else count[4]++;
              }
              average = sum / 45.0;
              rate = 1 - (score[4] / 45.0);
              System.out.println("成绩统计汇总如下:");
              System.out.println("最高分:" + score[44]);
              System.out.println("最低分:" + score[0]);
              System.out.println("及格率:" + rate);
              System.out.println("平均分:" + average);
              System.out.println("各分数段人数:");
              System.out.println("90分以上:" + count[0]);
              System.out.println("80~90分:" + count[1]);
              System.out.println("70~80分·" + count[2]);
              System.out.println("60~70分:" + count[3]);
              System.out.println("60分以下" + count[4]);
          }
      }
      
    5. 筛法求200以内的所有质数。

              int[] arr = new int[200];
              int i, j;
              for(i = 2; i < 200; i++) {
                  arr[i] = 1;
              }
              for(i = 2; i < 200; i++) {
                  if(arr[i] != 0) {
                      System.out.print(i + " ");
                      for(j = i; j < 200; j += i)
                          arr[j] = 0;
                  }
              }
      
    6. 排序

      • 选择法排序
        	int[] a = new int[10];
        		int i, j, t;
        		Random rd = new Random();
        		
        		System.out.println("随机生成的数组元素(100以内的整数)是:");
        		for(i = 0; i < a.length; i++) {
        			a[i] = rd.nextInt(100);
        			System.out.print(a[i] + " ");
        		}
        		System.out.println();
        		
        		for(i = 0; i < a.length; i++) {
        			for(j = i + 1; j < a.length; j++) {
        				if(a[j] < a[i]) {
        					t = a[i];
        					a[i] = a[j];
        					a[j] = t;
        				}
        			}
        		}
        		
        		System.out.println("按从小到大排序后是:");
        		for(i = 0; i < a.length; i++) {
        			System.out.print(a[i] + " ");
        		}
        		System.out.println();
        
        
        		int[] a = new int[10];
        		int i, j, t, minIndex;
        		Random rd = new Random();
        		
        		System.out.println("随机生成的数组元素(100以内的整数)是:");
        		for(i = 0; i < a.length; i++) {
        			a[i] = rd.nextInt(100);
        			System.out.print(a[i] + " ");
        		}
        		System.out.println();
        		
        		for(i = 0; i < a.length; i++) {
        			minIndex = i;
        			for(j = i + 1; j < a.length; j++) {
        				if(a[j] < a[minIndex]) {
        					minIndex = j;
        				}
        			}
        			t = a[i];
        			a[i] = a[minIndex];
        			a[minIndex] = t;
        		}
        		
        		System.out.println("按从小到大排序后是:");
        		for(i = 0; i < a.length; i++) {
        			System.out.print(a[i] + " ");
        		}
        		System.out.println();
        
        
      • 冒泡法排序
        		int[] a = new int[10];
        		int i, j, t;
        		Random rd = new Random();
        		
        		System.out.println("随机生成的数组元素(100以内的整数)是:");
        		for(i = 0; i < a.length; i++) {
        			a[i] = rd.nextInt(100);
        			System.out.print(a[i] + " ");
        		}
        		System.out.println();
        		
        		for(i = 0; i < a.length; i++) {
        			for(j = 0; j < a.length - i - 1; j++) {
        				if(a[j] > a[j+1]) {
        					t = a[j];
        					a[j] = a[j+1];
        					a[j+1] = t;
        				}
        			}
        			System.out.printf("第%d趟排序后是:", i+1);
        			for(int k = 0; k < a.length; k++) {
        				System.out.print(a[k] + " ");
        			}
        			System.out.println();
        		}
        		
        		System.out.println("按从小到大排序后是:");
        		for(int k = 0; k < a.length; k++) {
        			System.out.print(a[k] + " ");
        		}
        		System.out.println();
        
        
      • 插入法排序
        		int[] a = new int[10];
        		int i, j, t;
        		Random rd = new Random();
        		
        		System.out.println("随机生成的数组元素(100以内的整数)是:");
        		for(i = 0; i < a.length; i++) {
        			a[i] = rd.nextInt(100);
        			System.out.print(a[i] + " ");
        		}
        		System.out.println();
        		
        		for(i = 1; i < a.length; i++) {
        			t = a[i];
        			for(j = i - 1; j >= 0; j--) {				
        				if(t < a[j]) 
        					a[j + 1] = a[j];
        				else
        					break;
        			}
        			a[j+1] = t;
        			
        			System.out.printf("第%d趟排序后是:", i+1);
        			for(int k = 0; k < a.length; k++) {
        				System.out.print(a[k] + " ");
        			}
        			System.out.println();
        		}
        		
        		System.out.println("按从小到大排序后是:");
        		for(int k = 0; k < a.length; k++) {
        			System.out.print(a[k] + " ");
        		}
        		System.out.println();
        
        
      • 或直接排
         Arrays.sort(a);
        
    7. n个人围成一圈,轮流数数(从1开始数到num,再从1开始),数num的人退出圈,问最后剩下的人是谁。

      		int n;   //最初的人数
      		int num;  //数数的范围
      		int i, j, count;
      		Scanner sc = new Scanner(System.in);
      		System.out.print("请输入参与游戏的人数: ");
      		n = sc.nextInt();
      		System.out.print("请输入数数的值: ");
      		num = sc.nextInt();
      		int[] a = new int[n+1];
      		
      		a[0] = n;  //a[0]里存放圈内的人数
      		for(i = 1; i < n+1; i++) //第i个人在圈里,置为1,不在圈里置为0
      			a[i] = 1;
      		
      		i = 1;
      		count = 0;
      		while(a[0] > 1) {   //圈内人数>1人		
      			if(a[i] == 1) {	//a[i]==1表明第i个人在圈内					
      				count++;
      				if(count % num == 0) {	
      					a[i] = 0;  //a[i]置为0表明第i个人出圈
      					a[0]--;   //则人数-1
      
      					for(j = 0; j <= n; j++){					
      						System.out.print(a[j] + " ");
      					}
      					System.out.printf("\n");
      				}
      			}
      			if(i == n) { //i==N-1表明数到了最后一个人			
      				i = 0;	  //则将i拨到开始位置
      			}
      			i++;
      		}
      		
      		System.out.print("最后剩的人的号码是:");
      		for(i = 1; i <= n; i++)	{
      			if(a[i] == 1)
      				System.out.printf("%d\n", i);
      		}
      

二维数组

是一位数组的嵌套(每一行看做一个内层的一维数组)

二维数组的初始化

创建、声明与初始化

		int[][] a = new int[3][4];
        int[][] a;
        a = new int[3][4];
        int[][]a=new int[][]{{1,2,3},{4},{5,6,7},{8,9,10}};
        int[][]a=new int[3][];
        a[0]=new int[1];
        a[1]=new int[2];
        a[2]=new int[3];

输入

        int[][] a = new int[4][4];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < a.length; i++)
            for (int j = 0; j < a[i].length; j++) a[i][j] = sc.nextInt();

输出

        int[][] a = new int[3][4];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < a.length; i++)
            for (int j = 0; i < a[j].length; j++) System.out.println(a[i][j]);

示例

输出7排杨辉三角

public class Test {
    public static void main(String[] args) {
        int[][] a = new int[7][7];
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == i || j == 0) a[i][j] = 1;
                else a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
                if (j == i) System.out.print(a[i][j] + "\n");
                else if (a[i][j] != 0) System.out.print(a[i][j] + "\t");
            }
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值