第3内在while循环不工作

时间:2022-11-29 20:33:03

In the blow code the inner 3rd while loop not working please tell me why ? Here I tried with for loop by replacing the 3rd inner while loop it is working correctly but why not working with while loop .....? can you give me genuine reason...?

在打击代码内部第3个while循环不工作请告诉我为什么?在这里我尝试使用for循环替换第3内部while循环它正常工作,但为什么不使用while循环.....?你能给我真正的理由......?

import java.util.Scanner;
public class MergeArray {

    void arrayInitialization(Scanner arg) {
        //entering test cases
        System.out.println("enter test cases");
        int t = arg.nextInt();
        int k, l, i;
        k = 0;
        l = 0;
        i = 0;
        //outer while loop
        while (t-- > 0) {
            //initializing a1[]'s size
            System.out.println("enter a1[]'s size");
            int as1 = arg.nextInt();
            int a1[] = new int[as1];
            //inner while loop-1
            while (as1-- > 0) {
                System.out.println("enter a1[]'s elements");
                a1[i] = arg.nextInt();
                System.out.print(a1[i]);
                i++;
            }
            i = 0;
            //initializing a2[]'s size
            System.out.println("enter a2[]'s size");
            int as2 = arg.nextInt();
            int a2[] = new int[as2];
            //inner while loop-2
            while (as2-- > 0) {
                System.out.println("enter a2[]'s elements");
                a2[i] = arg.nextInt();
                System.out.print(a2[i]);
                i++;
            }
            System.out.println();
            int a3[] = new int[a1.length + a2.length];
            int size = as1 + as2;
            //inner while loop-3
            while (size-- > 0) {
                if (k < a1.length)
                    a3[l] = a1[k];
                if (k < a2.length)
                    a3[l + 1] = a2[k];
                k++;
                l += 2;
            }
            for (int j = 0; j < (a1.length + a2.length); j++) {
                System.out.print(a3[j]);
            }
        }
    }
    public static void main(String[] args) {
        MergeArray ma = new MergeArray();
        Scanner sc = new Scanner(System. in );
        ma.arrayInitialization(sc);
    }
}

I tried so much but not found solution. Here I am using while loop because I know that while loop will work fast instead of for loop.

我尝试了很多,但找不到解决方案。这里我使用while循环因为我知道while循环将快速工作而不是for循环。

2 个解决方案

#1


1  

It does not work because you are decrementing the sizes of as1 and as2. Which will be

它不起作用,因为您正在减小as1和as2的大小。这将是

 int size = as1 + as2; // size = 0 + 0;

Instead you can make use of the array length e.g.

相反,你可以使用数组长度,例如

int size = as1.length + as2.length;

#2


0  

Murat K's Answer is right, but try to init the arrays like this:

Murat K的答案是对的,但尝试初始化这样的数组:

        //init a1
        System.out.println("enter a1[]'s size");
        int a1[] = new int[arg.nextInt()];

        //fill a1
        for (int i = 0; i < a1.length; i++) {
            System.out.println("enter element " + i + " of a1[]");
            a1[i] = arg.nextInt();
        }

        //init a2
        System.out.println("enter a2[]'s size");
        int a2[] = new int[arg.nextInt()];

        //fill a2
        for (int i = 0; i < a2.length; i++) {
            System.out.println("enter element " + i + " of a2[]");
            a2[i] = arg.nextInt();
        }

        //init a3
        int a3[] = new int[a1.length + a2.length];

        //merge a1 and a2 into a3
        for (int i = 0; i < a1.length; i++) {
            a3[i] = a1[i];
        }
        for (int i = 0; i < a2.length; i++) {
            a3[a1.length + i] = a2[i];
        }

        //print
        for (int i : a3) {
            System.out.print(i);
        }

#1


1  

It does not work because you are decrementing the sizes of as1 and as2. Which will be

它不起作用,因为您正在减小as1和as2的大小。这将是

 int size = as1 + as2; // size = 0 + 0;

Instead you can make use of the array length e.g.

相反,你可以使用数组长度,例如

int size = as1.length + as2.length;

#2


0  

Murat K's Answer is right, but try to init the arrays like this:

Murat K的答案是对的,但尝试初始化这样的数组:

        //init a1
        System.out.println("enter a1[]'s size");
        int a1[] = new int[arg.nextInt()];

        //fill a1
        for (int i = 0; i < a1.length; i++) {
            System.out.println("enter element " + i + " of a1[]");
            a1[i] = arg.nextInt();
        }

        //init a2
        System.out.println("enter a2[]'s size");
        int a2[] = new int[arg.nextInt()];

        //fill a2
        for (int i = 0; i < a2.length; i++) {
            System.out.println("enter element " + i + " of a2[]");
            a2[i] = arg.nextInt();
        }

        //init a3
        int a3[] = new int[a1.length + a2.length];

        //merge a1 and a2 into a3
        for (int i = 0; i < a1.length; i++) {
            a3[i] = a1[i];
        }
        for (int i = 0; i < a2.length; i++) {
            a3[a1.length + i] = a2[i];
        }

        //print
        for (int i : a3) {
            System.out.print(i);
        }