Java笔记(2)——数组

2023-05-16

0. 数组的用法

数组的初始化 前面永远是空的
数组初始化完成,数组的长度是固定的

// 静态初始化:数组的初始化和数组的元素赋值同时进行
int[] arr = new int[]{1, 2, 3};
int[] arr2 = new int[10];
// 动态初始化:数组的初始化和元素的赋值分开进行
String[] str = new String[10];  

1. 求数组的长度

System.out.println(arr.length); // 3

2. 遍历数组

char c = new char[]{'C', 'h', 'i', 'n', 'a', ' ', 'C', 'i', 't', 'i', 'c', ' ', 'B', 'a', 'n', 'k'};
for(int i = 0; i < c.length; i++){
    System.out.print(c[i]);
}

//或者是下面的

for (char value : c) {
    System.out.print(value);
}
System.out.println();
//China Citic Bank

String[] str = new String[]{"China", " Citic", " Bank"};
for(String value : str){
    System.out.print(value);
}
/*
China Citic Bank
*/

3. 2维数组

2维数组的初始化

2维数组内部可以长度不一致,缺少的元素直接补0,但统计每行长度的时候,0不算在内,以具体数字为主

// 2维数组的定义
int[][] a, b;
int[] c[], d[]; // 都是可以的 但是不建议用
int[][] array = new int[2][3];
array[0][0] = 1;
array[0][1] = 2; 
//array[0][2] = 10; // 如果这里缺少实际元素  遍历的时候直接补0
array[1][0] = 3;
array[1][1] = 4;
array[1][2] = 5;

或者是初始化

int[][] arr = {{1, 2, 3}, {4, 5, 6}};
int[][] arr2 = int[][]{{4, 5, 6}, {7, 8, 9}};

4. 2微数组的遍历

for(int[] arr : array){
    for(int k : arr){
        System.out.println(k);
    }
}
/*
1
2
0
3
4
5
*/
//System.out.println(array[0][0]);
System.out.println("转化为字符串格式:" + Arrays.deepToString(array));
/*
转化为字符串格式:[[1, 2, 0], [3, 4, 5]]
*/

5. 打印杨辉三角

打印10行杨辉三角
1. 第一行有一个元素,第n行有n个元素
2. 每一行的第一个元素和最后一个元素都是1
3. 从第三行开始,对于非第一个元素和最后一个元素,即
yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j]
public class yanghuisanjiao {
    public static void main(String[] args) {
        int[][] yanghui = new int[10][];

        for(int i = 0; i < yanghui.length; i++){
            yanghui[i] = new int[i + 1]; //开辟空间
            yanghui[i][0] = 1;
            yanghui[i][i] = 1;
            for(int j = 1; j < yanghui[i].length - 1; j++)
                yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j];
        }

        // 杨辉三角10行的遍历
        System.out.println("打印前10行的杨辉三角:");
        for(int[] arr : yanghui){
            for(int i : arr){
                System.out.print(i + "  ");
            }
            System.out.println();
        }

    }
}
/*
打印前10行的杨辉三角:
1  
1  1  
1  2  1  
1  3  3  1  
1  4  6  4  1  
1  5  10  10  5  1  
1  6  15  20  15  6  1  
1  7  21  35  35  21  7  1  
1  8  28  56  70  56  28  8  1  
1  9  36  84  126  126  84  36  9  1  

Process finished with exit code 0
*/

6. 数组的工具

package com.company;

// 数组工具的使用

import java.util.Arrays;

public class arraytools {
    public static void main(String[] args){
        int[] arr1 = {1,2,3,4};
        int[] arr2 = {1,3,2,4};
        // 判断数组是否相等
        boolean isEquals = Arrays.equals(arr1, arr2);
        System.out.println("这两个数组是否相等:" + isEquals); // 这两个数组是否相等:false

        // 遍历数组
        System.out.println(Arrays.toString(arr1)); // [1, 2, 3, 4]

        // 将指定的值放入到数组中间
        Arrays.fill(arr1, 10);
        System.out.println(Arrays.toString(arr1)); //[10, 10, 10, 10]

        // 对数组进行排序
        Arrays.sort(arr2);
        System.out.println(Arrays.toString(arr2)); //[1, 2, 3, 4]

        // 对数组进行二分查找 前提  数组有序
        int[] arr3 = new int[]{-98, -34, 2, 34, 54, 65, 79, 100, -2, 0, 20, 45};
        Arrays.sort(arr3); // 对数组进行排序
        int index = Arrays.binarySearch(arr3, 100);
        System.out.println(index);
        if(index >= 0){
            System.out.println("找到了,在第" + index + "位!");
        } else{
            System.out.println("没有找到!");
        }

    }
}
/*
这两个数组是否相等:false
[1, 2, 3, 4]
[10, 10, 10, 10]
[1, 2, 3, 4]
11
找到了,在第11位!
*/

7.数组中算法的考察

package com.company;

/*
算法的考察  平均数 最大值 最小值 总和
生成随机数2位数 10个数字
 */

import com.sun.org.apache.bcel.internal.generic.SWAP;
import com.sun.org.apache.xerces.internal.dom.DeferredAttrImpl;
import com.sun.org.apache.xpath.internal.axes.ReverseAxesWalker;
import com.sun.xml.internal.ws.message.stream.StreamHeader12;
import sun.security.util.Length;

import java.nio.file.FileSystemLoopException;
import java.util.Arrays;

public class algorithmTest {
    public static void main(String[] args){
        int[] arr = new int[10];
        for(int i = 0; i < arr.length; i++)
            arr[i] = (int) (Math.random() * (99 - 10 + 1) + 10); // 生成2位数整数
        for(int i : arr)
            System.out.print(i + " ");
        System.out.println();

        // 求数组元素的最大值
        int maxValue = arr[0];
        for(int v : arr)
            if(v > maxValue)
                maxValue = v;
        System.out.println("arr数组的最大值是:" + maxValue);



        // 数组的反转

        String[] str = new String[]{"JJ", "DD", "MM", "YY", "NN"};
        System.out.println("str字符串数组中存储的内容是:" + Arrays.toString(str));
        String[] str1 = new String[str.length];
        System.arraycopy(str, 0, str1, 0, str.length);
        /*
        for(int i = 0 ; i < str.length; i++)
            str1[i] = str[i];
        */
        String temp;
        for(int i = 0, j = str1.length - 1; i < j; i++, j--){
            temp = str1[i];
            str1[i] = str1[j];
            str1[j] = temp;
        }
        System.out.println("str1字符串数组中存储的内容是:" + Arrays.toString(str1));

        // 数组的查找
        String dest = "FF";
        boolean flag = false;
        for (String s : str)
            if(s.equals(dest)){
                System.out.println("找到了,是" + dest);
                flag = true;
                break;
            }
        if(!flag)
            System.out.println("没有找到" + dest);

        // 冒泡排序
        int[] bubble = {23, 0, 9, -2, -5, 24, 4, -8, 4};
        for(int i = bubble.length - 1; i > 1; i--){
            for(int j = 0; j < i; j++){
                if(bubble[j] > bubble[j + 1]){
                    int t = 0;
                    t = bubble[j];
                    bubble[j] = bubble[j + 1];
                    bubble[j + 1] = t;
                }
            }
        }
        System.out.println("冒泡排序算法的结果是:" + Arrays.toString(bubble));

    }


}
/*
53 92 12 98 56 47 23 36 88 74 
arr数组的最大值是:98
str字符串数组中存储的内容是:[JJ, DD, MM, YY, NN]
str1字符串数组中存储的内容是:[NN, YY, MM, DD, JJ]
没有找到FF
冒泡排序算法的结果是:[-8, -5, -2, 0, 4, 4, 9, 23, 24]
*/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java笔记(2)——数组 的相关文章

随机推荐