java将偶数元素分配给偶数索引,将奇数分配给奇数位置,如果数字不相等,则在这些位置添加零

我正在尝试编写代码来显示偶数元素到偶数索引和奇数到奇数索引,如果添加的数字相同,则相应地添加零。

例子:

x = [1,2,3,4] 输出:2 1 4 3 x = [1 1 1 4] 输出:4 1 0 1 0 1

我达到了偶数和奇数位置,但在那之后就卡住了。

下面是我的代码。

import java.util.*;


class ArrayDemo3 {

    

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        System.out.println("Enter Size of Array :: ");

        int size = s.nextInt();

        int[] x = new int[size];


        System.out.println("Array Created having the size :: " + size);

        System.out.println("Enter Elements for Array :: ");

        for (int i = 0; i < size; i++) {

            System.out.println("Enter element no-" + (i + 1) + " ::");

            x[i] = s.nextInt();

        }


        System.out.println("Contents of Array ::");

        for (int i = 0; i < size; i++) {

            System.out.print(x[i] + "  ");

        }

        

        for (int i = 0; i < size; i = i + 1) {

            int even = 0;

            int odd = 1;

            if (i < size && x[i] % 2 == 0) {

                System.out.print("even : ");

                even = even + i;

                System.out.print("position" + i + " " + x[i] + "  ");

            } else {

                System.out.print("odd : ");

                odd = odd + i;

                System.out.print(i + " " + x[i] + "  ");

            }

            if (even < size && odd < size) { 

                int temp = x[even]; 

                x[even] = x[odd]; 

                x[odd] = temp; 

            } else {

                        

            }

            //System.out.print(x[i] + "  ");

        }

    }


}


明月笑刀无情
浏览 104回答 3
3回答

江户川乱折腾

您可以将问题分为三部分:首先创建两个列表,一个包含按遇到的顺序排列的偶数,另一个包含奇数:&nbsp; &nbsp; private static List<List<Integer>> createOddityLists(int... numbers) {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> numsList = Arrays.stream(numbers).boxed().collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; List<List<Integer>> numsByOddity = new ArrayList<List<Integer>>();&nbsp; &nbsp; &nbsp; &nbsp; numsByOddity.add(new ArrayList<>()); // List of odd numbers&nbsp; &nbsp; &nbsp; &nbsp; numsByOddity.add(new ArrayList<>()); // List of even numbers&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; numsList.forEach(num -> numsByOddity.get(num % 2).add(num));&nbsp; &nbsp; &nbsp; &nbsp; return numsByOddity;&nbsp; &nbsp; }用零 ( s) 填充两个列表中较短的一个,0使其与另一个列表的长度相等:&nbsp; &nbsp; private static void padShorterList(List<List<Integer>> numsByOddity) {&nbsp; &nbsp; &nbsp; &nbsp; int sizeDiff = numsByOddity.get(0).size() - numsByOddity.get(1).size();&nbsp; &nbsp; &nbsp; &nbsp; int listIndexToBePadded = sizeDiff < 0 ? 0 : 1;&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> padding = Collections.nCopies(Math.abs(sizeDiff), 0);&nbsp; &nbsp; &nbsp; &nbsp; numsByOddity.get(listIndexToBePadded).addAll(padding);&nbsp; &nbsp; }最后连接两个列表:&nbsp; &nbsp; private static List<Integer> joinLists(List<List<Integer>> numsByOddity) {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> resultList = new ArrayList<>(numsByOddity.get(1));&nbsp; &nbsp; &nbsp; &nbsp; for (int idx = 0; idx < numsByOddity.get(0).size(); idx++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultList.add(idx * 2, numsByOddity.get(0).get(idx));&nbsp; &nbsp; &nbsp; &nbsp; return resultList;&nbsp; &nbsp; }以下是完整的工作示例:public class ArrayRearrangement {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public static void main(String[] args) {//&nbsp; &nbsp; &nbsp; int[] result = rearrange(1, 2, 3, 4);&nbsp; &nbsp; &nbsp; &nbsp; int[] result = rearrange(1, 1, 1, 4);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.stream(result).boxed().collect(Collectors.toList()));&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private static int[] rearrange(int... numbers) {&nbsp; &nbsp; &nbsp; &nbsp; List<List<Integer>> numsByOddity = createOddityLists(numbers);&nbsp; &nbsp; &nbsp; &nbsp; padShorterList(numsByOddity);&nbsp; &nbsp; &nbsp; &nbsp; return joinLists(numsByOddity).stream().mapToInt(i->i).toArray();&nbsp; &nbsp; }&nbsp; &nbsp; private static List<List<Integer>> createOddityLists(int... numbers) {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> numsList = Arrays.stream(numbers).boxed().collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; List<List<Integer>> numsByOddity = new ArrayList<List<Integer>>();&nbsp; &nbsp; &nbsp; &nbsp; numsByOddity.add(new ArrayList<>()); // List of odd numbers&nbsp; &nbsp; &nbsp; &nbsp; numsByOddity.add(new ArrayList<>()); // List of even numbers&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; numsList.forEach(num -> numsByOddity.get(num % 2).add(num));&nbsp; &nbsp; &nbsp; &nbsp; return numsByOddity;&nbsp; &nbsp; }&nbsp; &nbsp; private static void padShorterList(List<List<Integer>> numsByOddity) {&nbsp; &nbsp; &nbsp; &nbsp; int sizeDiff = numsByOddity.get(0).size() - numsByOddity.get(1).size();&nbsp; &nbsp; &nbsp; &nbsp; int listIndexToBePadded = sizeDiff < 0 ? 0 : 1;&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> padding = Collections.nCopies(Math.abs(sizeDiff), 0);&nbsp; &nbsp; &nbsp; &nbsp; numsByOddity.get(listIndexToBePadded).addAll(padding);&nbsp; &nbsp; }&nbsp; &nbsp; private static List<Integer> joinLists(List<List<Integer>> numsByOddity) {&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> resultList = new ArrayList<>(numsByOddity.get(1));&nbsp; &nbsp; &nbsp; &nbsp; for (int idx = 0; idx < numsByOddity.get(0).size(); idx++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultList.add(idx * 2, numsByOddity.get(0).get(idx));&nbsp; &nbsp; &nbsp; &nbsp; return resultList;&nbsp; &nbsp; }}GitHub 上的完整代码希望这可以帮助。

收到一只叮咚

根据索引对元素进行排序,即如果元素是偶数,则它必须位于偶数位置,反之亦然&nbsp;int sortArrayByEvenOddIndex(int arr[]) {&nbsp; &nbsp; &nbsp;int n = arr.length;&nbsp; &nbsp; &nbsp;int res[] = new int[n];&nbsp; &nbsp; &nbsp;int odd = 1;&nbsp; &nbsp; &nbsp;int even = 0;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (arr[i] % 2 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;res[even] = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;even += 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;res[odd] = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;odd += 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;return&nbsp; res;&nbsp;}

长风秋雁

使用数组我们可以做到这一点。代码需要优化。&nbsp; &nbsp; public static int[]&nbsp; arrangeInEvenOddOrder(int[] arr)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Create odd and even arrays&nbsp; &nbsp; &nbsp; &nbsp; int[] oddArr = new int[arr.length];&nbsp; &nbsp; &nbsp; &nbsp; int[] evenArr = new int[arr.length];&nbsp; &nbsp; &nbsp; &nbsp; int oCount = 0, eCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; // populate arrays even and odd&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] % 2 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; evenArr[eCount++] = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oddArr[oCount++] = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int[] resArr = new int[oCount >= eCount?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2*oCount :&nbsp; 2*eCount-1];&nbsp; &nbsp; &nbsp; &nbsp; // populate elements upto min of the&nbsp; &nbsp; &nbsp; &nbsp; // two arrays&nbsp; &nbsp; &nbsp; &nbsp; for (int i =0; i < (oCount <= eCount?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2*oCount : 2*eCount ); i++ )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( i%2 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resArr[i] = evenArr[i/2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resArr[i] = oddArr[i/2];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // populate rest of elements of max array&nbsp; &nbsp; &nbsp; &nbsp; // and add zeroes&nbsp; &nbsp; &nbsp; &nbsp; if (eCount > oCount)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=2*oCount,j=0;i<2*eCount-1; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i%2 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resArr[i] = evenArr[oCount+j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resArr[i] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (eCount < oCount)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=2*eCount,j=0;i<2*oCount; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( i%2 != 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resArr[i] = oddArr[eCount+j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resArr[i] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return resArr;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java