简体   繁体   English

我如何优化以下代码,使其可以接受任何数组大小并给我结果

[英]How do i optimize below code so that it can accept any array size and give me the result

The below code displays the subarrays of the given array. 下面的代码显示给定数组的子数组。 But if i increase array size by one the below code fails to display all the subarrays, to make it successfully display all the arrays i need to add one more for loop. 但是,如果我将数组大小增加1,则以下代码无法显示所有子数组,要使其成功显示所有数组,我需要再添加一个for循环。 So how can i optmize code so that it display all the subarrays dynamically by not depending on the array size. 因此,我如何优化代码,使其不依赖于数组大小而动态显示所有子数组。

public class Subarrays {

    public static void main(String[] args)
    {
        int[] arr=new int[] {1,-2,4,-5,1};
        int count=0;
        for(int i=0; i<arr.length; i++) {
            System.out.println("["+arr[i]+"]");
            for(int j=0; j<arr.length-(i+1); j++) {
            }
        }
        for(int i=0; i<arr.length-1; i++) {
            System.out.println("["+arr[i]+","+arr[i+1]+"]");
        }

        for(int i=0; i<arr.length-2;i++)
        {
            System.out.println("["+arr[i]+","+arr[i+1]+","+arr[i+2]+"]");
        }

        for(int i=0; i<arr.length-3; i++) {
            System.out.println("["+arr[i]+","+arr[i+1]+","+arr[i+2]+","+arr[i+3]+"]");
        }

        for(int i=0; i<arr.length-4; i++) {
            System.out.println("["+arr[i]+","+arr[i+1]+","+arr[i+2]+","+arr[i+3]+","+arr[i+4]+"]");
        }



    }


}
public static void printAllSubArrays(int[] arr) {
    for (int len = 1; len <= arr.length; len++) {
        for (int from = 0; from + len <= arr.length; from++) {
            System.out.print('[');

            for (int i = from; i < from + len; i++) {
                if (i != from)
                    System.out.print(',');
                System.out.print(arr[i]);
            }

            System.out.println(']');
        }
    }
}

Demo: 演示:

printAllSubArrays(new int[] { 1, -2, 4, -5, 1 });

Output: 输出:

[1]
[-2]
[4]
[-5]
[1]
[1, -2]
[-2, 4]
[4, -5]
[-5, 1]
[1, -2, 4]
[-2, 4, -5]
[4, -5, 1]
[1, -2, 4, -5]
[-2, 4, -5, 1]
[1, -2, 4, -5, 1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重写我的代码,以使 0 的值不会在 java 中给我一个越界错误? - How do I rewrite my code so that the value of 0 does not give me a out of bounds error in java? 如何让数组方法为我提供正确的输出? - How do I get the array method to give me the correct output? 如何创建 object 数组并在 java 中获取用户输入? 我写了下面的代码,但让我很困惑 - How can I create an array of object and takes user input in java? I have wrote the below code but confusing me 我如何优化蜡染 svg 使其可以生成小文件 - How can i optimize batik svg so it can generate small size file 我怎么做到这样我可以从任何类访问这个数组? - How do I make it so I can access this array from any class? 我正在将图像作为数组类型的字节。 如何只接受特定范围的图像尺寸? - I am taking image as byte of array type. How can I do accept only specific range of image size? 如何优化此代码? - How can I optimize this code? 我需要修改我的代码以在我的 POST 请求中接受 json 对象的多个列表。 我们如何实现这一点? 任何建议都会帮助我 - I need to modify my code to accept multiple list of json objects in my POST request. How do we implement this ? Any suggestion would help me 为什么这段代码给我使用Interface的编译时错误,以及如何实现此代码? 请解释? - Why this code give me compile Time error using Interface and how i can implement this code? Please Explain? 为什么使用如此多的CPU声明一个数组,我该如何优化它? - Why is declaring an array using so much CPU, and how can I optimize it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM