You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java数组排序追加及线性搜索最小值后循环终止问题

Fixing Java Array Sort/Append & Linear Search Termination Issues

Alright, let's work through your Java array problems step by step—fixing the linear search termination issue and getting the sorted array append logic right to hit your desired output of {1,2,8,10,12,30}.

First: Fixing the Linear Search Termination

You mentioned that after finding the minimum value (1), the loop keeps running and replaces 1 with 3, leading to an output of 333101 instead of your expected result. Let's break this down:

Scenario 1: You know the target minimum value (1) and want to stop at the first occurrence

If you already know 1 is the value you're looking for and just need to grab the first instance of it then stop, the fix is simple: use a break statement as soon as you find the value. This prevents the loop from continuing and overwriting your result.

Here's how to implement that:

int[] a = {3, 3, 3, 1, 0, 1}; // Your input array
int[] b = {2, 8, 10, 12, 30}; // The array you want to append to

int targetMin = 1;
boolean found = false;

for (int num : a) {
    if (num == targetMin) {
        // Append the found value to array b
        b = appendElementToArray(b, num);
        found = true;
        break; // This stops the loop immediately after finding the value
    }
}

// Helper method to append an element to an array
public static int[] appendElementToArray(int[] arr, int element) {
    int[] newArr = new int[arr.length + 1];
    System.arraycopy(arr, 0, newArr, 0, arr.length);
    newArr[arr.length] = element;
    return newArr;
}

That break is the key here—it cuts the loop short so later elements (like those 3s) can't overwrite the 1 you just found.

Scenario 2: You need to find the actual global minimum of array a

If you don't know the minimum value upfront and need to calculate it from the array, you'll need to loop through all elements to find the smallest one, then append it to b. No need to break early here—you have to check every element to confirm the true minimum:

int[] a = {3, 3, 3, 1, 0, 1};
int[] b = {2, 8, 10, 12, 30};

int minValue = a[0]; // Start with the first element as the initial minimum
for (int num : a) {
    if (num < minValue) {
        minValue = num; // Update the minimum if we find a smaller value
    }
}

// Append the calculated minimum to array b
b = appendElementToArray(b, minValue);

Next: Sorting an Array & Appending It to Another

To sort an array and append all its elements to another array (or just append the sorted minimum and sort the combined array to hit your desired output), here's how to do it:

Option: Append the target minimum from a to b, then sort the combined array (matches your desired output)

Since your expected output is a sorted array containing 1 plus your original b elements, this is the approach you want:

import java.util.Arrays;

public class ArrayHelper {
    public static void main(String[] args) {
        int[] a = {3, 3, 3, 1, 0, 1};
        int[] b = {2, 8, 10, 12, 30};

        // Step 1: Find the first occurrence of 1 and append to b
        int targetMin = 1;
        for (int num : a) {
            if (num == targetMin) {
                b = appendElementToArray(b, num);
                break; // Stop after first match
            }
        }

        // Step 2: Sort the combined array to get your desired output
        Arrays.sort(b);

        // Print the result
        System.out.println(Arrays.toString(b)); // Output: [1, 2, 8, 10, 12, 30]
    }

    public static int[] appendElementToArray(int[] arr, int element) {
        int[] newArr = new int[arr.length + 1];
        System.arraycopy(arr, 0, newArr, 0, arr.length);
        newArr[arr.length] = element;
        return newArr;
    }
}

This code will produce exactly the output you're aiming for: {1,2,8,10,12,30}.


内容的提问来源于stack exchange,提问作者Gopika Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:25:48