简体   繁体   English

返回仅包含奇数整数的数组

[英]return an Array which contains only odd integers

Ok, so I have this problem where when given an Array arr, return an Array which contains only odd integers in the original order from arr. 好的,所以我有这个问题,当给定一个数组arr时,返回一个数组,该数组仅包含来自arr的原始顺序中的奇数整数。

My code: 我的代码:

public int [] youMakeMeOdd(int [] arr)
{
   int[] odds;
   odds = new int[arr.length];
   for(int i = 0; i < arr.length; i++)
   {
      if(arr[i] % 2 != 0)
      {
         odds[i] = arr[i];
      }
   }
   return odds;
}

Few Testers: 很少有测试人员:

Expected...........................................................Run: 预期................................................. ..........跑:

youMakeMeOdd({1,2,3}) → {1, 3}.....................{1, 0, 3} youMakeMeOdd({1,2,3})→{1,3} ..................... {1,0,3}

youMakeMeOdd({2,1,3,5,7}) → {1, 3, 5, 7}.......{0, 1, 3, 5, 7} youMakeMeOdd({2,1,3,5,7})→{1,3,5,7} ....... {0,1,3,5,7}

youMakeMeOdd({2,4,6,8}) → {}........................{0, 0, 0, 0} youMakeMeOdd({2,4,6,8})→{} ........................ {0,0,0,0}

.

I can't seem to figure out how to put a blank space there instead of 0's. 我似乎无法弄清楚如何在此处放置空格而不是0。 Help appreciated, thanks :) 感谢帮助,谢谢:)

The output array is being initialized to the size of the input array. 输出数组被初始化为输入数组的大小。 I guess this being java code, the array elements are initialized to zero by default. 我猜这是Java代码,默认情况下将数组元素初始化为零。 So whenever the if condition does skips the ith position the default value (zero) is being shown. 因此,只要if条件跳过第i个位置,就会显示默认值(零)。

public int[] youMakeMeOdd(int [] arr) {
   List<Integer> odds = new ArrayList<Integer>();
   for(int i = 0; i < arr.length; i++)
   {
      if(arr[i] % 2 != 0)
      {
         odds.add(arr[i]);
      }
   }
   return convertIntegers(odds);
}

public static int[] convertIntegers(List<Integer> integers)
{
    int[] ret = new int[integers.size()];
    Iterator<Integer> iterator = integers.iterator();
    for (int i = 0; i < ret.length; i++)
    {
        ret[i] = iterator.next().intValue();
    }
    return ret;
}

You could have a pre-computation loop where you just increment a counter and then allocate odds: 您可能会有一个预计算循环,您只需增加一个计数器然后分配几率即可:

int counter = 0;
for (int i = 0; i < arr.length; i++)
{
    if (arr[i] % 2 != 0)
    {
        counter ++;
    }
}
odds = new int[counter];
var result = input.Select(a=>a % 2 != 0 ? a : 0).ToArray()

您可以轻松地使用linq ,基本上是使用原始数组,然后使用Select选择数组本身的值或选择0(如果它是偶数),然后使用ToArray方法转换为array

I would use an ArrayList. 我会使用ArrayList。 Your problems seems to be the fact that arrays are immutable, so you it automatically fills your array with a bunch of unneeded 0s. 您的问题似乎是数组是不可变的,因此您会自动用一堆不需要的0填充数组。 ArrayLists can change dimensions, so you don't have to have the 0s. ArrayLists可以更改尺寸,因此您不必设置0。

You can have a look at them here: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 您可以在这里查看它们: http : //docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Make sure you import the util package. 确保导入util软件包。

 import java.util.*;



public ArrayList<Integer>  youMakeMeOdd(int [] arr)
{

You need to specify the type that you want to hold in the angle braces. 您需要在尖括号中指定要保留的类型。 Because int is a primitive, you need to use the Integer class 因为int是原始类型,所以您需要使用Integer类

   ArrayList<Integer> odds;
   odds = new ArrayList<>();
   for(int i = 0; i < arr.length; i++)
   {
      if(arr[i] % 2 != 0)
      {

The add method adds an integer to the end add方法将一个整数添加到末尾

         odds.add(new Integer(arr[i]));
      }
   }
   return odds;
}

暂无
暂无

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

相关问题 如何从奇数和偶数整数数组中仅对奇数整数排序并仅显示排序的奇数整数? - How to sort only odd integers from an array of both odd and even integers and only display the sorted odd integer? 整数数组,如果有 2 个连续的数字是 7 或有两个 7 被数字分隔,则返回 true。 仅使用 Java 流 - array of integers,return true if there are 2 consecutive numbers which are 7 or there are two 7s separated by a number . Using Java Streams only 将偶数和奇数整数添加到数组中 - Adding even and odd integers into Array 字符串仅包含2个整数-Java - String only contains 2 integers - Java 计算数组中奇数个数的递归方法 - Recursive method to count number of odd integers in array 检查数组是否包含某些整数 - Checking if an array contains certain integers 给定一个字符串数组,返回仅包含回文的字符串数组 - Given an Array of strings, return an array of strings that contains only the palindromes 给定一个+ ve整数数组,找到在O(n)时间和常数空间中出现奇数次的数字。 - Given an array of +ve integers, Find the number which occurs odd number of times in O(n) time & constant space. 如何返回包含两个 arrays 中出现的值的数组? 而且没有重复 - How to return an array that contains values, which appear in two arrays? And no duplicates Java - 转换用户的输入,其中包含整数和双精度数组 - Java - Casting user's input, which contains both integers and doubles into an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM