/*
* Given an array of positive ints, return a new array of length "count" containing the
* first even numbers
* from the original array. The original array will contain at least "count" even numbers.
*/
public class StringEx
{
public static void main(String[] args)
{
int[] nums = {2,3,5,6,8};
int count = 2;
StringEx s1 = new StringEx();
System.out.println(s1.copyEvens(nums, count));
}
public int[] copyEvens(int[] nums, int count)
{
int[] n=new int[count];
int c=0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]%2==0&&c!=count)
{
n[c]=nums[i];
c++;
}
}
return n;
}
}
// Output:[I@87816d
Asked
Active
Viewed 216 times
1

user2357112
- 260,549
- 28
- 431
- 505

Nandan
- 23
- 4
1 Answers
6
Arrays don't have a nice toString
method. They use Object.toString
, which gives
getClass().getName() + '@' + Integer.toHexString(hashCode())
which is usually unhelpful. Use Arrays.toString
if you want a readable representation.

user2357112
- 260,549
- 28
- 431
- 505
-
How do i get to know the result? This System.out.println(s1.copyEvens(Arrays.toString(nums), count)); is not acceptable for sure! – Nandan Mar 06 '14 at 07:33
-
1@Nandan: You're `toString`ing the wrong thing. Convert the `copyEvens` return value, not the input. – user2357112 Mar 06 '14 at 07:58