Summary of the mistakes in Niuke's real questions JAVA

Posted by prosolutions on Fri, 07 Feb 2020 14:11:13 +0100

Which of the following methods is used to define the thread's execution body (): run()

==The memory address is judged. integer can call cache directly when it gives int value between - 127 and 128, so if the memory address is the same, an object will be new if it exceeds this range, and false will be returned if the memory address is different

It's logical shift to the left, fill 0 to the right, and move the sign bit like other ones. Mathematical meaning: on the premise that the number does not overflow, for positive and negative numbers, shift one bit to the left is equal to the 1 power multiplied by 2, and shift n bit to the left is equal to the n power multiplied by 2

The synchronization mechanism provided by java for I/O access is filter flow.

If you want to list all files with the extension ". txt" in the current directory and subdirectory, you can use which of the following commands:
The Linux find command is used to find files in the specified directory.

A series of algorithms are defined, and each algorithm is encapsulated so that they can replace each other.

The capacity of virtual memory is limited only by the number of computer address bits.

If the processor has a 32-bit address, the 32 power of its virtual address space bit 2 = 4G

According to the definition of binary tree, there are five kinds of binary trees with three nodes

The main data structure used in cache system is hashMap

The relationship and difference of ArrayList, vector and linkllist in java language
The same: ArrayList, LinkedList and Vector all implement the java.util and list interfaces. In the same java.util package, they all allow Null values. The difference between ArrayList and Vector is that they store data in the way of array. The number of elements in this array is larger than the actual data to add and insert elements. They all allow direct ordinal index elements, but the inserted data should be designed into array elements Because of memory operations such as prime move, index data is fast and data insertion is slow. Because Vector uses synchronized method, it is thread safe, so its performance is worse than ArrayList. LinkedList uses two-way linked list to realize storage. Index data by sequence number needs to be traversed forward or backward. However, when inserting data, only the front and back items of this item need to be recorded, so the insertion speed is faster! Both ArrayList and LinkedList are not thread safe.

What are the disadvantages of unlimited thread creation?
1. The cost of thread life cycle is very high, and the creation and destruction of threads are costly. The creation process of threads takes time, and the JVM and operating system are required to provide some auxiliary operations. If the arrival rate of requests is very high and the processing process of requests is lightweight, creating a new thread per request will consume a lot of computing resources; 2. Resource consumption . Active threads will consume system resources, especially memory. If the number of running threads is more than the number of available processors, these threads will be idle, and a large number of idle threads will occupy a lot of memory, which will bring pressure to the garbage collector, and a large number of threads will generate other performance overhead when competing for CPU resources. 3. Stability: there is a limit on the number of threads that can be created. If these limits are broken, an OutOfMemoryError exception may be thrown. It is very dangerous to recover from these errors. A simpler way is to construct a program to avoid exceeding these limits.

There is a stairway with n steps in height. You can only go up one or two steps at each step. It is required to use a program to find out how many steps there are in total.

import java.util.Scanner;

public class jieti {

    public static void main(String[] args1)
    {
        Scanner sc = new Scanner(System.in);
        int i= sc.nextInt();
        int count = qiuJie(i);
        System.out.println(count);
    }
    private static int qiuJie(int i)
    {
        if(i == 1|| i == 0)
            return 1;
        else
            return qiuJie(i-1)+qiuJie(i-2);
    }
}

Create two threads, one output 1-52 and the other output A-Z. Output format requirements: 12A 34B 56C 78D

import java.util.concurrent.atomic.AtomicInteger;

public class twoThread {
    public static volatile boolean flag = false;
    public static AtomicInteger time = new AtomicInteger();
    public static void main(String[] args)
    {
        new Thread(()->
        {
            for(int i=1;i<53;i=i+2)
            {
                if(!flag&&(time.get()==0||time.incrementAndGet()%2==0))
                {
                    System.out.println(i);
                    System.out.println(i+1);
                    flag=true;
                }
                else
                {
                    i=i-2;
                }
            }
        }).start();
        new Thread(
                ()->
                {
                    for(int i=0;i<26;i++)
                    {
                        if(flag&&(time.get()==1||time.incrementAndGet()%2==1))
                        {
                            System.out.println((char)(i+'A'));
                            System.out.println(' ');
                            flag=false;
                        }
                        else
                        {
                            i--;
                        }
                    }
                }
        ).start();
    }
}

Turn the order of the words in the sentence in place, but the order of the contents of the words does not change.

import java.util.Scanner;

public class transformDanci {
    public static void main(String[] argc)
    {
        Scanner sc = new Scanner(System.in);
        String target = sc.nextLine();
        String reserve = reserve(target);
        System.out.println(reserve);

    }
    public static String reserve(String str)
    {
        String []ss =str.split("");
        StringBuffer s = new StringBuffer("");
        for(int i = ss.length-1;i>=0;i--)
        {
            s.append(ss[i]+" ");
        }
        return s.toString();
    }
}

Input an array of positive integers, link them up and arrange them into a number, and output the smallest of all the numbers that can be discharged.

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class getMinNum {
    public static void main(String[] argc)
    {
        ConnectArray();
    }
    public static void ConnectArray()
    {
        String str=new Scanner(System.in).next();
        String[] numbers = str.split(",");
        Arrays.sort(numbers, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(o1.length() == o2.length())
                {
                    return o1.compareTo(o2);
                }
                return (o1+o2).compareTo((o2+o1));
            }
        });
        for(String s:numbers)
        {
            System.out.printf("%s",s);
        }
    }
}

For N items, their weight W is W1,w2,wn, their value is v1,v2 vn there is only one item in each item. Now I will give you a backpack with a weight of M to find the maximum value of the items in the backpack

    import java.util.Scanner;

    public class tanxin {
        public static void main(String[] argc)
        {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int d = sc.nextInt();
            int[] value=new int[n+1];
            int[] weight=new int[n+1];
            for(int i=1;i<=n;i++)
            {
                weight[i]=sc.nextInt();
            }
            for(int i=1;i<=n;i++)
            {
                value[i]=sc.nextInt();
            }
            int[][]dp = new int[n+1][d+1];
            dp[0][d]=0;
            for(int i =1;i<=n;i++)
            {
                for(int j=d;j>=weight[i];j--)
                {
                    dp[i][j]=Integer.max(dp[i-1][j],dp[i-1][j-weight[i]]+value[i]);
                }
            }
            System.out.println(dp[n][d]);
        }
    }

Published 9 original articles, praised 0, visited 205
Private letter follow

Topics: Java Linux jvm