# 随机字符出现在我的输出中，不确定它们来自哪里。

go评论46阅读模式

Random characters showing up in my output, not sure where they're coming from

# 问题

``````public class ArrayMethods1 {
public static int[] printOdds(int[] arrayExample) {
int i;
String oddNum = "";
int[] newArray = new int[3];
int x = 0;
for (i = 0; i < arrayExample.length; i++) {
if (arrayExample[i] % 2 != 0) {
System.out.print(arrayExample[i] + " ");
}
}
int[] sumOfOdds = new int[1];
sumOfOdds = sumOdds(newArray);
return sumOfOdds;
}
public static int[] sumOdds(int[] arrayExample1) {
int i;
int[] oddsTotal = new int[1];
int total = 0;
for (i = 0; i < arrayExample1.length; i++) {
if (arrayExample1[i] % 2 != 0) {
total = total + arrayExample1[i];
}
}
oddsTotal[0] = total;
return oddsTotal;
}
public static void main(String[] args) {
int[] mainArray = new int[5];
mainArray[0] = 17;
mainArray[1] = 92;
mainArray[2] = 21;
mainArray[3] = 984;
mainArray[4] = 75;
printOdds(mainArray);
int[] oddSum = new int[1];
oddSum = sumOdds(mainArray);
System.out.println(oddSum);
}
}
``````

``````17 21 75 [I@51016012
``````

I'm creating a program that uses two methods to print all the odd numbers in an array and then get the sum of the odd numbers. However, I'm getting an output that makes no sense. This is the code that I'm using:

``````public class ArrayMethods1 {
public static int[] printOdds(int[]arrayExample) {
int i;
String oddNum = &quot;&quot;;
int [] newArray = new int [3];
int x = 0;
for (i = 0; i &lt; arrayExample.length; i++) {
if (arrayExample[i] % 2 != 0) {
System.out.print(arrayExample[i] + &quot; &quot;);
}
}
int[] sumOfOdds = new int [1];
sumOfOdds = sumOdds(newArray);
return sumOfOdds;
}
public static int[] sumOdds(int[]arrayExample1) {
int i;
int[] oddsTotal = new int[1];
int total = 0;
for (i = 0; i &lt; arrayExample1.length; i++) {
if (arrayExample1[i] % 2 != 0) {
total = total + arrayExample1[i];
}
}
oddsTotal[0] = total;
return oddsTotal;
}
public static void main(String[] args) {
int [] mainArray = new int [5];
mainArray[0] = 17;
mainArray[1] = 92;
mainArray[2] = 21;
mainArray[3] = 984;
mainArray[4] = 75;
printOdds(mainArray);
int [] oddSum = new int[1];
oddSum = sumOdds(mainArray);
System.out.println(oddSum);
}
}
``````

And I'm getting this as output:
17 21 75 [I@51016012

I have absolutely no idea where that second part is coming from, so any help would be awesome. Thanks!

# 答案1

well you are storing the result of the sum in an array and then you print the reference of type int[], that's why you get [I@51016012. so you need to print oddSum[0].

# 答案2

``````public static void printOdds(int[] arr) {
for (int n : arr) {
if (n % 2 == 1) {
System.out.print(n + " ");
}
}
System.out.println();
}

public static int sumOdds(int[] arr) {
int total = 0;
for (int n : arr) {
if (n % 2 == 1) {
total += n;
}
}
}
``````

``````import java.util.Arrays;

public class PrintAndSumOdds {

public static void main(String [] args){
int[] arr = {17, 92, 21, 984, 75};

int sumOdds = Arrays.stream(arr) // 从数组获取IntStream
.filter(n -> n % 2 == 1)   // 过滤出奇数
.peek(n -> System.out.print(n + " "))  // 在一行中打印它们
.sum(); // 计算总和（终端操作）
System.out.println("\nTotal odds: " + sumOdds);
}
}
``````

``````17 21 75
Total odds: 113
``````

It is not quite clear why you return `int[]` from the methods that just print and calculate the sum of the odd numbers.

So the code could be enhanced:

``````public static void printOdds(int[] arr) {
for (int n : arr) {
if (n % 2 == 1) {
System.out.print(n + &quot; &quot;);
}
}
System.out.println();
}
public static int sumOdds(int[] arr) {
int total = 0;
for (int n : arr) {
if (n % 2 == 1) {
total += n;
}
}
}
``````

Also, it may be worth to use Java 8+ stream to implement both tasks in one run:

``````import java.util.Arrays;
public class PrintAndSumOdds {
public static void main(String [] args){
int[] arr = {17, 92, 21, 984, 75};
int sumOdds = Arrays.stream(arr) // get IntStream from the array
.filter(n -&gt; n % 2 == 1)   // filter out the odds
.peek(n -&gt; System.out.print(n + &quot; &quot;))  // print them in one line
.sum(); // and count the sum (terminal operation)
System.out.println(&quot;\nTotal odds: &quot; + sumOdds);
}
}
``````

Output:

``````17 21 75
Total odds: 113
``````

• 本文由 发表于 2020年7月23日 03:40:55
• 转载请务必保留本文链接：https://go.coder-hub.com/63041991.html
• java

go 43

go 44

go 41

go 37