Java learning Chapter array

Array definition

① When the large number of variables appear to store data, enter several numbers, enter several coordinate points, create multiple variables to store these data will become more troublesome. Since the type of these variables is substantially common, we can manage all the numbers with a container. Similar to the string, the string is actually several containers only. Then we will call this container array.

Mainly to solve the multi-array data storage problems multivariate uniform procedures to facilitate post-maintenance data.

The nature of the array is a series of address space equal and continuous piece of storage space.

Equal space: is to maintain the unity of our data, it must ensure that the data are consistent between types of (multiple variables of the same type together combinations called array).

② array is an address space the size of a continuous and qualitative storage space (but space for each stored address or other data)

Present in the heap memory array, the data stored in the memory called stack objects created in the heap will have a default initial value of the object

Ⅰ default integer type 0

Ⅱ floating-point types default 0.0

Boolean Default false

Reference data types (objects) default null

Providing access to data elements of the array subscripts among

Address array variable is stored in an array of heap memory first element

DETAILED calculation array elements accessed by subscripts are: the address to be accessed by the first data element = address + data size of the subscript * (it is equivalent to our arithmetic sequence).

Once the array down, he can not change the length; the length of the elements of the array spatial array ==

Size or content must be clearly defined when creating arrays ③

Ⅰ Data Type [] = new Array Data Name [Type Length]; (only the length, but creates an array of the specified content is not specified)

class Test04{
    public static void main(String[] args){
        //需求1:创建长度为5的int型数组(一维)  矩阵(二维)
        int[] arr=new int[5];
        //需求2:访问arr数组中第4个元素
        System.out.println(arr[3]);//1000+3*4
        System.out.println(arr[0]);
        System.out.println(arr[2]);
        //创建长度为5的String型数组
        String[] strArr=new String[5];
        System.out.println(strArr[0]);

        //System.out.println(arr[10]);
        //ArrayIndexOutOfBoundsException 角标越界
        int[] arr2=arr;
        //此时此刻 数组还是那个数组对象 
        //只不多有两个变量引用到了而已
        arr2[0]=10;
        System.out.println(arr[0]);
        arr2=null;
        //System.out.println(arr2[0]);
        //NullPointerException 空指针异常
        arr=null;
        //此时此刻 数组对象没有任何变量引用它
        //数组对象在堆内存中就没有存在的意义了
        //所以该对象变成垃圾,由【垃圾回收器gc】处理
        //【垃圾回收期】是JVM中的一个程序 专门用于负责处理堆内存中垃圾数据的
        //垃圾的处理并不是及时的,有【gc】来控制,当垃圾堆攒到一定程度时由【gc】来处理
        //特殊的 在C/C++中 如果出现对象垃圾 必须由程序员手动处理 free()及时处理
    }
}

Ⅱ Data Type [] = new Array Name Data Type [] {1,2,3,4,5} (specified content creates an array (specified length))

Ⅲ Data Type [] = {1,2,3,4,5} array name (specified content creates an array (specified length))

IV [] represents a one-dimensional array

Ⅴ [] [] represents a two-dimensional array

An array of common errors

ArrayIndexOutOfBoundsException array subscript out of bounds

NullPointerException null pointer exception

Basic Operations

Traverse, assignment, Maximum Minimum

Its code is expressed as:

import java.util.Scanner;
public class HHH {

	public static void main(String[] args) {
		bianli();
		fuzhi();
		minmax();
	}
	public static void bianli () {
		int [] arr = {1,2,3,4,5,6,7,8,9};//角标1-8
		for (int i = 0;i < arr.length;i++) {//遍历
			System.out.println(arr[i]);
		}
	}
	public static void fuzhi() {
		Scanner in = new Scanner(System.in);
		System.out.print("请输入10个数字:");
		int arr2[] = new int [10];
		for (int i = 0;i < arr2.length;i++) {
			arr2[i] = in.nextInt();
		}
		for (int i = 0;i < arr2.length;i++) {
			System.out.print(arr2[i] + " ");
		}
	}
	public static void minmax() {
		int[] arr={8,5,9,6,1,4,2,3,5,3};
        int max=arr[0];
        int min_index=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]>max){
                max=arr[i];
            }
            if(arr[i]<arr[min_index]){
                min_index=i;
            }
        }
        System.out.println("最大值"+max);
        System.out.println("最小值角标"+min_index);

	}

}

Find operation

Traversal search, binary search, Fibonacci search.

 public static void main(String [] args){//遍历
       
        int[] arr={10,2,8,3,1,6,4,7,9,5};
        int key=11;
        int index=-1;   //key元素不存在
        for(int i=0;i<arr.length;i++){
            if(arr[i]==key){
                index=i;
                break;
            }
        }
        System.out.println(index);
    }
}
public class Erfen {

	public static void main(String[] args) {
	     int [] arr = {1,2,3,4,5,6,7,8,9};
	     int search = 5;
	     int min = 0;
	     int max = arr.length - 1;
	     int mid = (min + max) / 2;
	  
	     while (arr[mid] != search) {
	    	 if (arr[mid] > search) {
	    		 max= mid-1;
	    	 }
	    	 if (arr[mid] < search) {
	    		 min = mid+ 1;
	    	 }
	    	
	    	 mid = (min + max) / 2;
	    	 if (min > max) {
		    		System.out.print("无查询结果");
		    		return;
		    	 }
	     }
	     System.out.println(mid);
	    }
}

Sequence

class Testpaixu{
    public static void main(String[] args){
        //1.选择排序O(n^2)
        selectSort();
        //2.冒泡排序O(n^2)
        bubbleSort();
        //3.插入排序O(n^2) 
        insertSort();
        //4.计数排序O(n+m)
        /*
        上述三个排序都是根据数据之间的大小关系进行比较排序的
        计数 基数 桶 都是根据数据本身的特性比较 与大小无关的排序
        这些排序只针对整数
        是一个典型的牺牲空间换时间的排序
        */
        countSort();

    }
    public static void countSort(){
        int[] arr={8,5,9,2,7,4,6,1,3,10,-3,-2,-10};
        int min=arr[0];
        int max=arr[0];
        for(int i=0;i<arr.length;i++){//O(n)
            if(arr[i]>max){
                max=arr[i];
            }
            if(arr[i]<min){
                min=arr[i];
            }
        }
        int[] nums=new int[max-min+1];
        int offset=min;
        for(int i=0;i<arr.length;i++){//O(n)
            nums[arr[i]-offset]++;
        }
        int index=0;
        for(int i=0;i<nums.length;i++){//O(m)
            if(nums[i]!=0){
                for(int j=0;j<nums[i];j++){
                    arr[index++]=i+offset;
                }
            }
        }
        show(arr);
    }
    public static void insertSort(){
        int[] arr={8,5,9,2,7,4,6,1,3};
        int e;
        int j;
        for(int i=1;i<arr.length;i++){
            e=arr[i];
            for(j=i;j>0&&arr[j-1]>e;j--){
                arr[j]=arr[j-1];
            }
            arr[j]=e;
        }
        /*
        for(int i=1;i<arr.length;i++){
            for(int j=i;j>0&&arr[j-1]>arr[j];j--){
                swap(arr,j,j-1);
            }
        }
        */
        show(arr);
    }
    //如何优化自己看 反正我讲了 你们自己回去写代码 不要求
    public static void bubbleSort(){
        int[] arr={8,5,9,2,7,4,6,1,3};
        for(int i=0;i<arr.length-1;i++){//-1是少一轮比较
            for(int j=0;j<arr.length-1-i;j++){//-1避免重复比较和角标越界
                if(arr[j]>arr[j+1]){
                    swap(arr,j,j+1);
                }
            }
        }
        show(arr);
    }
    public static void selectSort(){
        int[] arr={8,5,9,2,7,4,6,1,3};
        for(int i=0;i<arr.length-1;i++){//-1是因为没有必要进行最后一个数字的比较
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]>arr[j]){
                    swap(arr,i,j);//即用-即释放
                }
            }
        }
        show(arr);
    }
    public static void swap(int[] arr,int i,int j){
        //1.借助三方变量进行交换 
        //适用于所有的数据类型 比较通用
        /*
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
        */
        //2.借助加减法运算进行交换
        //只适用于数字相关的数据类型
        arr[i]=arr[i]+arr[j];
        arr[j]=arr[i]-arr[j];
        arr[i]=arr[i]-arr[j];

        //3.借助位运算进行交换
        //只适用于整数相关的数据类型
        /*
        int a=3;
        int b=7;
        a=a^b;
            0011
            0111
            0100
        b=a^b;
            0100
            0111
            0011 ->3
        a=a^b;
            0100
            0011
            0111 ->7
        */
    }
    public static void show(int[] arr){
        //[1,2,3,4,5,6,7,8,9]
        String s="[";
        for(int i=0;i<arr.length;i++){
            if(i==arr.length-1){
                s+=arr[i]+"]";
            }else{
                s+=arr[i]+",";
            }
        }
        System.out.println(s);
    }
}

When arranged in descending sort is inserted: if the current of the digital numbers on the left, and the current number is greater than the left side of the digital exchange.

Counting sort of thought:

Bubble sort is from left to right in order to compare the two.

If sorting is selected after all the elements of the current element is compared, if the former is larger than the latter, then the switch.

import java.util.*;
class Demo05_01{
    public static void main(String[] args){
        /*
        思路1:数组长度不固定 需要读取一个数据 数组扩容 填入数据
        数据填入之后进行排序 然后遍历数组依次判断数据的个数
        连续相等
        2 2 2 2 3 3 4 4 4 4 5 5 6 6 6 6 7 7
        思路2:借助计数排序的思想 将数组固定起来
        */
        //1.获取用户输入的数据 动态的扩容数组填充数据
        Scanner scanner = new Scanner(System.in);
        int[] arr=new int[0];
        System.out.print("Enter numbers:");
        while(true){
            int num=scanner.nextInt();
            if(num==0){
                break;
            }
            //验证用户输入数据的正确性
            if(num<1||num>100){
                System.out.println("有非法数据!");
                return;
            }
            arr=copyOf(arr,arr.length+1);
            arr[arr.length-1]=num;
        }
        //2.按照输出结果 将数据中的数据进行排序
        insertSort(arr);
        //3.输出连续相等的数字
        show(arr);
    }
    public static int[] copyOf(int[] arr,int newLen){
        int[] newArr=new int[newLen];
        for(int i=0;i<arr.length;i++){
            newArr[i]=arr[i];
        }
        return newArr;
    }
    public static void insertSort(int[] arr){
        for(int i=1;i<arr.length;i++){
            int e=arr[i];
            int j;
            for(j=i;j>0&&arr[j-1]>e;j--){
                arr[j]=arr[j-1];
            }
            arr[j]=e;
        }
    }
    /*
    Arrays Math都是属于工具类
    Arrays 特殊的是数组的工具类
        toString(arr) 就是将数据的每个元素进行拼接 并返回拼接后的字符串数据
        "[1,2,3,4]"
    */
    public static void show(int[] arr){
        System.out.println(Arrays.toString(arr));
        //此时就将问题转成了如何判断连续相等的数据分别出现多少次
        //[1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 5, 5, 6]
        for(int i=0;i<arr.length;){
            int count=1;
            for(int j=i+1;j<arr.length;j++){
                if(arr[j]==arr[i]){
                    count++;
                }else{
                    break;
                }
            }
            System.out.println(arr[i]+" occurs "+count+(count>1?" times":" time"));
            i+=count;
        }
    }
}

 

import java.util.*;
class Demo05_02{
    public static void main(String[] args){
        /*
        思路1
            在全部输入之后去重复 func1
        思路2
            边输入边去重复 func2
        */
        // func1();
        func2();
    }
    public static void func2(){
        int[] arr=new int[0];
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter numbers:");
        for(int i=0;i<10;i++){
            int num=scanner.nextInt();
            if(!contains(arr,num)){
                arr=copyOf(arr,arr.length+1);
                arr[arr.length-1]=num;
            }
        }
        System.out.println(Arrays.toString(arr));
    }
    public static void func1(){
        //1.循环遍历数组进行赋值
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter numbers:");
        int[] arr = new int[10];
        for(int i = 0;i < arr.length;i++){
            arr[i] = scanner.nextInt();
        }
        //2.开始对已有的数据进行去重复操作
        // 1 2 3 3 2 4 3 2 4 1
        // 1 2 3 4
        // method1(arr);        //空间S(n) 时间O(nm)
        // method2(arr);        //空间S(1) 时间O(n^2)
        // method3(arr);
    }
    public static void method3(int[] arr){
        //不创建额外空间 不许改变原先的顺序
        int i=0;
        int size=arr.length;
        while(i<size){
            for(int j=i+1;j<size;){
                if(arr[j]==arr[i]){
                    for(int k=j+1;k<size;k++){
                        arr[k-1]=arr[k];
                    }
                    size--;
                }else{
                    j++;
                }
            }
            i++;
        }
        for(i=0;i<size;i++){
            System.out.print(arr[i]+" ");
        }
    }
    public static void method2(int[] arr){
        //插入排序
        for(int i=1;i<arr.length;i++){
            int e=arr[i];
            int j;
            for(j=i;j>0&&arr[j-1]>e;j--){
                arr[j]=arr[j-1];
            }
            arr[j]=e;
        }
        //连续相等
        for(int i=0;i<arr.length;){ //O(n)
            System.out.print(arr[i]+" ");
            int count=1;
            for(int j=i+1;j<arr.length;j++){
                if(arr[j]==arr[i]){
                    count++;
                }else{
                    break;
                }
            }
            i+=count;
        }
    }
    public static void method1(int[] arr){
        int[] newArr=new int[0];
        for(int i=0;i<arr.length;i++){ //O(n)
            if(!contains(newArr,arr[i])){ //O(m)
                newArr=copyOf(newArr,newArr.length+1);
                newArr[newArr.length-1]=arr[i];
            }
        }
        System.out.println(Arrays.toString(newArr));
    }
    public static boolean contains(int[] arr,int key){
        for(int i=0;i<arr.length;i++){
            if(arr[i]==key){
                return true;
            }
        }
        return false;
    }
    public static int[] copyOf(int[] arr,int newLen){
        int[] newArr=new int[newLen];
        for(int i=0;i<arr.length;i++){
            newArr[i]=arr[i];
        }
        return newArr;
    }

}

 5.3

import java.util.*;
class Demo05_03{
    public static void main(String[] args){
        //1.获取用户的输入 只不过第一个输入的数据时数据的个数(数组的长度)
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter a list:");
        int len=scanner.nextInt();//获取的第一个数值就是数组的长度
        int[] arr=new int[len];
        for(int i=0;i<arr.length;i++){
            arr[i]=scanner.nextInt();
        }
        //2.对数组进行有序的判断
        if(isSorted(arr)){
            System.out.println("The list is already sorted.");
        }else{
            System.out.println("The list is not sorted.");
        }
    }
    public static boolean isSorted(int[] list){
        //如果不是升序排列 那么势必会出现有一组数据 左大右小的情况
        for(int i=1;i<list.length;i++){
            if(list[i-1]>list[i]){
                return false;
            }
        }
        return true;
    }

}

import java.util.*;
class Demo05_04{
    /*
    输入的数据:槽子的个数 球的个数=路径的个数
               创建槽子的具体的容器int[]
               每一个小球下落的路径L R 字符串
               对于每一个小球而言其路径中的步骤是随机产生L R
    1.提示用户输入槽子的个数和小球的个数
    2.根据已有的槽子的个数去创建槽子容器
    3.根据已有的球和槽子的个数去随机创建一个小球下落的路径
    4.路径中经过几个钉子?路径的步骤有几步 和槽子的个数有关
    5.如何通过路径的经过得知最终所落入的槽子?
    */
    public static void main(String[] args){
        //1.
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter the number of balls to drop:");
        int balls=scanner.nextInt();
        System.out.print("Enter the number of slots in the bean machine:");
        int slots=scanner.nextInt();
        //2.
        int[] arr=new int[slots];
        //3.几个球几个路径path
        for(int i=0;i<balls;i++){
            String path=getPath(slots);
            System.out.println(path);
            //5.只要看当前路径中R的个数即可
            arr[getR(path)]++;
        }
        //6.输出
        System.out.println(Arrays.toString(arr));
        show(arr);
    }
    public static void show(int[] arr){
        int w=arr.length;
        int h=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]>h){
                h=arr[i];
            }
        }
        for(int i=h-1;i>=0;i--){
            for(int j=0;j<w;j++){
                if(i<arr[j]){
                    System.out.print("O");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }
    public static int getR(String path){
        int count=0;
        for(int i=0;i<path.length();i++){
            if(path.charAt(i)=='R'){
                count++;
            }
        }
        return count;
    }
    public static String getPath(int slots){
        //4.根据槽子的个数计算每一个球下落的路径
        Random random=new Random();
        String path="";
        for(int j=0;j<slots-1;j++){
            if(random.nextInt(2)==0){   //向左
                path+="L";
            }else{  //向右
                path+="R";
            }
        }
        return path;
    }
}

 

class Demo05_05{
    public static void main(String[] args){
        int[] list1={1,2,3,4,5,6,7};
        int[] list2={1,2,3,4,5,7,6};
        System.out.println(equals(list1,list2));
    }
    public static boolean equals(int[] list1,int[] list2){
        //判断两个数组是否完全相同
        //1.先判断长度
        if(list1.length!=list2.length){
            return false;
        }
        //2.再依次判断元素大小
        for(int i=0;i<list1.length;i++){
            if(list1[i]!=list2[i]){
                return false;
            }
        }
        return true;
    }
}

class Demo05_06{
    public static void main(String[] args){
        int[] arr={1,1,1,1,2,2,2,2,2,3,3,3,3,3,4};
        for(int i=0;i<arr.length;){
            int count=1;
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]==arr[j]){
                    count++;
                }else{
                    break;
                }
            }
            if(count>=4){
                System.out.println(arr[i]);
                return;
            }
            i+=count;
        }
        System.out.println("没有!");
    }
}

 

import java.util.*;
class Demo05_07{
    public static void main(String[] args){
        int[] list1={1,3,5,7,9};
        int[] list2={2,4,6,8,10};
        System.out.println(Arrays.toString(merge(list1,list2)));
    }
    /*
    有序数组的合并
    最主要的问题在于 数组之间有长有短
    */
    public static int[] merge(int[] list1,int[] list2){
        if(list1==null&&list2==null){
            return null;
        }
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }
        //只有两个都不是null的情况再考虑具体操作
        int[] list3=new int[list1.length+list2.length];
        int p1=0;
        int p2=0;
        int p3=0;
        while(true){
            if(p1==list1.length&&p2==list2.length){
                break;
            }
            if(p1<list1.length&&p2==list2.length){
                list3[p3++]=list1[p1++];
            }else if(p1==list1.length&&p2<list2.length){
                list3[p3++]=list2[p2++];
            }else{
                if(list1[p1]<=list2[p2]){
                    list3[p3++]=list1[p1++];
                }else{
                    list3[p3++]=list2[p2++];
                }
            }
        }
        return list3;
    }
}

 

 

 

import java.util.*;
class Demo05_08{
    /*
    数据 一组单词的明文  单词的密文  单词的状态
    program
    1000000 r r
    pr**r**
    */
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        Random random=new Random();
        //1.创建一个单词表
        String[] words={"naruto","kakashi","sasuke","banana","java","program"};
        //10.最后再去做多单词猜测
        while(true){
            //2.随机从单词表中抽取一个单词
            String word=words[random.nextInt(words.length)];
            //3.创建一个该单词的状态表 默认值是false(密文)
            boolean[] status=new boolean[word.length()];
            int miss=0; //猜错的个数
            //4.开始猜一个单词
            while(true){
                //5.根据单词和状态表 决定密文形式
                String ciphertext=getCipherText(word,status);
                //6.输出密文并提示用户输入字母
                System.out.print("Enter a letter in word "+ciphertext+" >");
                char letter=scanner.nextLine().charAt(0);//"p".charAt(0)
                //7.判断单词中是否有该字母
                if(isContainsLetter(word,letter)){
                    //8.改变单词状态表 已修改/未修改 
                    //true 表示从未修改 第一次来的
                    //false 表示已修改  不是第一次来 提示已经存在
                    if(!changeWordStatus(word,status,letter)){
                        System.out.println("\t "+letter+" is already in the word");
                    }
                }else{
                    System.out.println("\t "+letter+" is not in the word");
                    miss++;
                }
                //9.是否结束
                if(isFinish(status)){
                    System.out.println("The word is "+word+". You miss "+miss+" time");
                    break;
                }
            }
            System.out.print("Do you want to guess another word?Enter y or n:");
            String choice=scanner.nextLine();
            if(choice.equals("n")){
                System.out.println("Welcome!Thank you! FUCK PROGRAM!");
                break;
            }
        }
    }
    public static boolean isFinish(boolean[] status){
        for(int i=0;i<status.length;i++){
            if(!status[i]){
                return false;
            }
        }
        return true;
    }
    public static boolean changeWordStatus(String word,boolean[] status,char letter){
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)==letter){
                if(status[i]){
                    return false;   //说明已经修改
                }else{
                    status[i]=true;
                }
            }
        }
        return true;
    }
    public static boolean isContainsLetter(String word,char letter){
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)==letter){
                return true;
            }
        }
        return false;
    }
    public static String getCipherText(String word,boolean[] status){
        String ciphertext="";
        for(int i=0;i<status.length;i++){
            if(status[i]){
                ciphertext+=word.charAt(i);
            }else{
                ciphertext+="*";
            }
        }
        return ciphertext;
    }
}

Eight Queens

class EightQueen{
    //n皇后如何处理?n>=4
    public static int count=0;
    public static int n;
    public static void main(String[] args){
        n=8;
        int[][] board=new int[n][n];
        //0就是空 1就是皇后
        eightQueen(board,0);
    }
    //解决board在第level层的八皇后问题 level 0~7
    public static void eightQueen(int[][] board,int level){
        if(level==n){   //如果递归到了第9行 则当前是一个解
            count++;
            System.out.printf("这是第%d个解:\n",count);
            for(int i=0;i<board.length;i++){
                for(int j=0;j<board[i].length;j++){
                    System.out.print(board[i][j]+" ");
                }
                System.out.println();
            }
        }else{
            //1.先做一份上一个情况的备份
            int[][] newBoard=new int[n][n];
            for(int i=0;i<board.length;i++){
                for(int j=0;j<board[i].length;j++){
                    newBoard[i][j]=board[i][j];
                }
            }
            //2.遍历当前行 找到所有可能的解
            for(int col=0;col<n;col++){
                //3.判断当前位置是否可以放皇后
                //  3.1 如果可以 则继续往下一行去寻找
                //  3.2 如果不行 则继续判断一下个位置
                if(isNoDanger(newBoard,level,col)){
                    //在赋值皇后之前 先当前行清空
                    for(int c=0;c<n;c++){
                        newBoard[level][c]=0;
                    }
                    newBoard[level][col]=1; //当前给皇后
                    eightQueen(newBoard,level+1);//接着下一行找
                }
            }
        }
    }
    //判断board当中 x y的位置上是否可以放置一个皇后
    public static boolean isNoDanger(int[][] board,int x,int y){
        //正上
        for(int r=x-1;r>=0;r--){
            if(board[r][y]==1){
                return false;
            }
        }
        //左上
        for(int r=x-1,c=y-1;r>=0&&c>=0;r--,c--){
            if(board[r][c]==1){
                return false;
            }
        }
        //右上
        for(int r=x-1,c=y+1;r>=0&&c<n;r--,c++){
            if(board[r][c]==1){
                return false;
            }
        }
        return true;
    }
}

 

Published 11 original articles · won praise 0 · Views 253

Guess you like

Origin blog.csdn.net/yihjh/article/details/104329646