A complete set of in-depth exploration of Java arrays - advanced knowledge stage 4, one-dimensional array exercises

A complete set of in-depth exploration of Java arrays - advanced knowledge stage 4, one-dimensional array exercises


Table of contents

The importance of array learning

Topic 1: Sum of arrays

Question 2: Find the maximum value in an array

Question 3. Sum of two numbers

Suggested practice question bank


General article link:https://laoshifu.blog.csdn.net/article/details/134906408

The importance of array learning

Arrays are one of the data structures that we must master, and they will be of great help to us in the future.

  • Improve program efficiency: Array is an efficient data structure that can quickly access and modify data. In actual production and life, arrays are widely used in various scenarios that require efficient data processing, such as image processing, scientific computing, financial analysis, etc. By learning arrays, students can process data more efficiently and improve program execution efficiency.
  • Enhance programming abilities: Arrays are one of the commonly used data structures in programming. Mastering the use of arrays is very important for students to improve their programming abilities. In the actual programming process, the use of arrays is very common. Mastering the use of arrays can help students become more proficient in programming and improve programming efficiency and code quality.
  • Cultivate logical thinking: Array is an abstract data structure. By learning arrays, students can develop their logical thinking abilities. In actual problem solving, many problems can be transformed into array processing problems. By learning arrays, students can think about problems more clearly and come up with effective solutions.

Learning arrays can be a somewhat difficult task for students, but if you keep studying, you can master it. Here are some words of encouragement for students learning about arrays:

  • Arrays are the basis of programming, and mastering the use of arrays is very important to becoming a good programmer.
  • Learning arrays can be difficult, but if you stick with it, you'll be able to master it.
  • By learning arrays, you can process data more efficiently, improve program execution efficiency, and show your programming abilities.
  • Arrays are widely used. Mastering the use of arrays can make you better in your future study and work.
  • Believe in yourself, you will be able to master the use of arrays and become an excellent programmer!

Topic 1: Sum of arrays

Topic description:

Given an array of integers, find the sum of all elements in the array.

enter:

An array of integers, for example: [1, 2, 3, 4, 5]

Output:

The sum of all elements in the array, for example: 15

Answer:

    public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 4, 5 };

        int sum = 0;
        for (int num : arr) {
            sum += num;
        }
        System.out.println("数组之和是:" + sum);
    }

Question 2: Find the maximum value in an array

Topic description:

Given an array of integers, find the maximum value in the array.

enter:

An array of integers, for example: [1, 5, 3, 7, 2]

Output:

Maximum value in the array, for example: 7

Answer:

public class Demo1 {
    public static void main(String[] args) {
        int[] arr = { 1, 5, 3, 7, 2 };
        int max = arr[0]; // 假设第一个元素是最大的
        for (int num : arr) {
            if (num > max) {
                max = num; // 更新最大值
            }
        }
        System.out.println("数组中的最大值是:" + max);
    }
}

Question 3. Sum of two numbers

Given an integer array nums and an integer target value target, please find the two integers in the array whose sum is the target value target and return their array subscripts.

You can assume that each input will correspond to only one answer. However, the same element in the array cannot appear repeatedly in the answer.

You can return answers in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, return [0, 1].
Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

hint:

2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 < ;= target <= 109
There will only be one valid answer

public class Demo1 {
    public static void main(String[] args) {
        int[] nums = { 3,2,4 };
        int target = 6;
        int[] array = new int[0];
        int n = nums.length;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (nums[i] + nums[j] == target) {
                    array = new int[] { i, j };
                }
            }
        }
        for (int i : array) {
            System.out.println(i);
        }
    }
}

Suggested practice question bank

For algorithm practice question banks for array training, I recommend the following:

LeetCode: This is a very well-known online algorithm question bank that contains array-related questions of various difficulty levels. By practicing these questions, you can improve your array-related algorithm design and coding abilities.

HackerRank: This is another popular online algorithm question bank that also contains a large number of array-related questions. The questions on this platform are relatively difficult and are suitable for practitioners with a certain foundation.

Niuke.com: This is a well-known online programming and algorithm learning platform in China, which provides a large number of array-related questions and exercises. This platform is characterized by high quality questions, moderate difficulty, and is suitable for practitioners of all levels.

The above three platforms all provide a wealth of array-related questions and exercises, which can help you improve your array-related algorithm design and coding abilities.

Guess you like

Origin blog.csdn.net/feng8403000/article/details/134910368