java数组(一维二维)、冒泡排序、与字符查找

一、数组

一、数组介绍:

在这里插入图片描述

Ps:
	int[] a ={
    
     ... };  or  int a[]  ={
    
     ... };
	for( int i = 0; i < 10; i++)
		{
    
    
        System.out.println(a[i]);
    		循环语句;
		}

变量类型+[ ]+变量名+{ 对数组进行赋值 }

二、数组的使用

在这里插入图片描述

1、动态初始化数组的两种方法:

(1)int a[];//声明数组
	a = new int[10]; //分配内存空间
(2)int a[] = new int[10];

在这里插入图片描述

2、

在这里插入图片描述

字长测试函数:
//length 对象是 数组,即[]
//length() 对象是 字符串,即“ ”
 //size() 对象是泛型集合,例如:TreeSet ,ArrayList等

三、数组

(1)、数组赋值

1、数组在默认情况下是引用传递,赋的是地址,赋值方式为引用赋值

​ (数组存放在堆当中,引用类型拷贝的是地址)

在这里插入图片描述

(2)、数组拷贝
int[] arr1 = {
    
    10,20,30};
int[] arr2 = new int[arr1.length];
for(int i;i<arr1.length;i++)
{
    
    
    arr2[i] = arr[i];
}

在这里插入图片描述

(3)、数组反转

在这里插入图片描述

在这里插入图片描述

我写的:数组翻转
public class nixushuzufuzhi {
    
    
    public static void main(String[] args) {
    
    
        int[] a = {
    
    1, 2, 3};
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; i++) {
    
    
            b[a.length - i - 1] = a[i];

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

(4)、数组扩容
public class Array_add {
    
    
    public static void main(String[] args) {
    
    
        int[] a = {
    
    1, 2, 3};
        int[] anew = new int[a.length + 1];
        for (int i = 0; i < a.length; i++) {
    
    
            anew[i] = a[i];
        }
        anew[anew.length - 1] = 4;
        a = anew;
        for (int i = 0; i < a.length; i++) {
    
    
            System.out.println(a[i]);
        }

    }
}
(5)冒泡排序
class bubblcsart {
    
    
    public static void main(String args[]) {
    
    
        int[] arr = {
    
    -5, 1, 4, 2, 8};
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
    
    
            for (int j = 0; j < arr.length - i - 1; j++) {
    
    
                if (arr[j] > arr[j + 1]) {
    
    
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.println(arr[i]);
        }
    }
}
(6)字符查找

String a=“字符”;

a.equals(字符)

在这里插入图片描述

おすすめ

転載: blog.csdn.net/m0_46319477/article/details/121587332