本帖最后由 c523106 于 2015-5-13 00:57 编辑
笔记内容:数组
笔记来源:毕向东Java基础视频第4天
正文:
一、数组的定义:存储同一种数据类型的多个元素的容器。
二、定义格式:
1:数据类型[] 数组名;
2:数据类型 数组名[];(了解即可)
三、数组的初始化
1、数据类型[] 数组名=new 数据类型[数组长度];
举例:int[] arr = new int[3];
2、数据类型[] 数组名=new 数据类型[]{存储元素};
举例:int[] arr = new int[]{1,2,3};
简写:int[] arr = {1,2,3};
四、数组的常见操作
1.遍历,获取最值,逆序,查找
- package cn;
- import java.util.Scanner;
- /**
- * 数组遍历(依次输出数组中的每一个元素)
- * 数组获取最值(获取数组中的最大值最小值)
- * 数组元素逆序 (就是把元素对调)
- * 数组查表法(根据键盘录入索引,查找对应星期)
- * 数组元素查找(查找指定元素第一次在数组中出现的索引)
- */
- public class ArrayTest {
- public static void main(String[] args) {
- int[] arr = { 1, 2, 3, 4, 3, 6, 3 };
- // method1(arr);
- // getMax(arr);
- // reverseArr(arr);
- // find();
- findIndex(3 ,arr);
- }
- public static void findIndex(int num,int[] arr) {
- // TODO Auto-generated method stub
- int count=0;
- for(int x:arr){
- if(x==num){
- System.out.println(count);
- break;
- }
- count++;
- }
- }
- public static void find() {
- // TODO Auto-generated method stub
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入索引:");
- int index = sc.nextInt();
- int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
- System.out.println(arr[index - 1]);
- sc.close();
- }
- public static void reverseArr(int[] arr) {
- // TODO Auto-generated method stub
- for (int x = 0; x < arr.length / 2; x++) {
- int temp = arr[x];
- arr[x] = arr[arr.length - x - 1];
- arr[arr.length - x - 1] = temp;
- }
- method1(arr);
- }
- public static void getMax(int[] arr) {
- // TODO Auto-generated method stub
- int max = 0;
- for (int x : arr) {
- if (max < x) {
- max = x;
- }
- }
- System.out.println(max);
- }
- public static void method1(int[] arr) {
- // TODO Auto-generated method stub
- for (int x : arr) {
- System.out.println(x);
- }
- for (int x = 0; x < arr.length; x++) {
- System.out.println(arr[x]);
- }
- }
- }
复制代码 2.二分查找
- package cn;
- /**
- * 二分查找。(针对有序数组,不要为了用二分查找,而排序)
- * @author cz
- *
- */
- public class Test6 {
- public static void main(String[] args) {
- int[] array={1,2,3,5,6,8,9};
- System.out.println(binarySearch(6,array));
- }
- public static int binarySearch(int i, int[] array) {
- // TODO Auto-generated method stub
- int max=array.length-1;
- int min=0;
- int mid=(max+min)/2;
- while(i!=array[mid]){
- if(i>array[mid]){
- min=mid+1;
- }else if(i<array[mid]){
- max=mid-1;
- }
- mid=(max+min)/2;
-
- }
- return mid;
- }
- }
复制代码 3、排序
- package cn;
- /**
- * 冒泡排序和选择排序
- * @author cz
- *
- */
- public class Test4 {
- public static void main(String[] args) {
- int[] array={1,32,23,11,67,9};
- bubbleSort(array);
- selectSort(array);
- for(int x:array){
- System.out.println(x);
- }
- }
- public static void bubbleSort(int[] array) {
- // TODO Auto-generated method stub
- for(int x=0;x<array.length;x++){
- for(int y=0;y<array.length-x-1;y++){
- if(array[y]>array[y+1]){
- int temp=array[y];
- array[y]=array[y+1];
- array[y+1]=temp;
- }
- }
- }
-
- }
- public static void selectSort(int[] array) {
- // TODO Auto-generated method stub
- for(int x=0;x<array.length;x++){
- for(int y=x+1;y<array.length;y++){
- if(array[x]>array[y]){
- int temp=array[x];
- array[x]=array[y];
- array[y]=temp;
- }
- }
- }
- }
- }
复制代码 4、二维数组遍历
- package cn;
- /**
- * 二维数组的遍历。
- * 公司年销售额求和
- * 某公司按照季度和月份统计的数据如下:单位(万元)
- * 第一季度:22,66,44
- * 第二季度:77,33,88
- * 第三季度:25,45,65
- * 第四季度:11,66,99
- *杨辉三角。
- *1
- *1 1
- *1 2 1
- *1 3 3 1
- *1 4 6 4 1
- *1 5 10105 1
- * @author cz
- *
- */
- public class Array2 {
- public static void main(String[] args) {
- int[][] arr = new int[][] { { 1, 3, 5 }, { 2, 4, 6 } };
- getArray(arr);
- // getArr2(6);
- }
- public static void getArray(int[][] arr) {
- // TODO Auto-generated method stub
- // int sum = 0;
- for (int[] array : arr) {
- for (int x : array) {
- System.out.println(x);
- // sum += x;
- }
- }
- for (int x = 0; x < arr.length; x++) {
- for (int y = 0; y < arr[x].length; y++) {
- if (arr[x][y] == 0) {
- break;
- }
- System.out.print(arr[x][y] + "\t");
- }
- System.out.println();
- }
- // System.out.println(sum);
- }
- public static void getArr2(int n) {
- int[][] arr = new int[n][n];
- for (int x = 0; x < arr.length; x++) {
- arr[x][0] = 1;
- arr[x][x] = 1;
- }
- for (int x = 2; x < arr.length; x++) {
- for (int y = 1; y <= x - 1; y++) {
- arr[x][y] = arr[x - 1][y] + arr[x - 1][y - 1];
- }
- }
- getArray(arr);
- }
- }
复制代码
|