Five, Java array example summary

Analysis of ideas:

method one:

Difficulties in that the array length is not fixed, a need to read data, and the need to count the number

          To a number and a number, then you need the expansion. Then fill in the data, and then sorted. Finally, sequentially through the array determines the number of data

          Expansion of the array, is to create a new array, the elements of the assignment in, the address of the new array to return

          1. Expansion: Create an array, and the original values ​​of the array assignment in, traversing a small array, a small element of the array individually by location into a new array.

          2. Read the data will be stored into the array element in
Method two:

With the idea of ​​counting sort, the array is fixed up.

Arrays are special tools array, toString (arr) is each element of the data string and returns the spliced ​​data splicing "[1,2,3,4]"

//方法一
public static void main(String[] args){
        int[] arr=new int[0];
        //1.读取元素将数据存放到数组中
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter numbers:");
        int num=0;
        while(true){
            num=scanner.nextInt();
            if(num==0){
                break;
            }
            arr=copyOf(arr,arr.length+1);
            arr[arr.length-1]=num;
        }
    }
    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;
    }
}

方法二:
import java.util.Scanner;
class Demo05_01_02{
    public static void main(String[] args){
        //0 1 2 3 4 5 ~ 100
        //0 单独不算 
        //arr[i] 就表示数字i出现的次数
        int[] arr=new int[101];
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter numbers:");
        while(true){
            int num=scanner.nextInt();
            if(num==0){
                break;
            }
            arr[num]++;
        }
        for(int i=0;i<arr.length;i++){
            if(arr[i]!=0){
                System.out.println(i+" occurs "+arr[i]+(arr[i]>1?" times":" time"));
            }
        }
    }
}

 

Ideas analysis

method one:

After all the input to repeat

 1. loop through the array assignment

 2. Start the existing data deduplication operations

 3. Do not create extra space allowed to change the original order

 Method Two:

Side input side deduplication         

Three methods method1 (arr): Do not create extra space allowed to change the original order 

              method2 (arr): insertion sort consecutive equal

              method3 (arr): determining whether the same


import java.util.*;
class Demo05_02{
    public static void main(String[] args){
        /*
        思路1   在全部输入之后去重复 func1
        */
        func1();
    }
    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();
        }
    }
    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;
    }
}



import java.util.*;
class Demo05_02{
    public static void main(String[] args){
        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 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 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;
    }
}

 

 Ideas analysis

    1. The user is prompted for the number of the trough and the number of balls
    2. The trough to create a container according to the number of existing trough
    according to the existing number of balls and the trough to create a random path a falling ball
    4. the path after a few nails? There are a few number of steps and the path of the trough about
    5. how paths that ultimately falls through the trough?

 输入的数据:槽子的个数 球的个数=路径的个数
                       创建槽子的具体的容器int[]
                       每一个小球下落的路径L R 字符串
                       对于每一个小球而言其路径中的步骤是随机产生L R

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;
    }
}

 

思路分析

根据用户输入的数字,判断是否有四个连续相等的数字,返回true或false

1.提示用户输入列表中数字的总数和具体的数字

2.判断列表中是否含有四个连续相等的数

 

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;
    }
}

 

思路分析

数据 一组单词的明文  单词的密文  单词的状态
    program
   1000000

输入:r 
    pr**r**

步骤:

1.创建一个单词表

2.随机从单词表中抽取一个单词

3.创建一个该单词的状态表 默认值是false(密文)

4开始猜一个单词

5.根据单词和状态表 决定密文形式

6.输出密文并提示用户输入字母

7.判断单词中是否有该字母

8.改变单词状态表 已修改/未修改
                    true 表示从未修改 第一次来的
                    false 表示已修改  不是第一次来 提示已经存在

 9.是否结束
 

 

import java.util.*;
class Demo05_08{
    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;
    }
}

 

发布了9 篇原创文章 · 获赞 0 · 访问量 179

Guess you like

Origin blog.csdn.net/q1220668269/article/details/104413140