[英]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.