java在一个方法中获取数组的所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23160584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java get all values of an array in a method
提问by Speaky
I'm creating a method to return values of an array. But, I have a problem.
我正在创建一个方法来返回数组的值。但是,我有一个问题。
Let's assume the array values are: 771 11 141 1 11 79 7
让我们假设数组值是: 771 11 141 1 11 79 7
arr[0] = 771, arr[1] = 11, arr[2] = 141,...arr[n]
arr[0] = 771, arr[1] = 11, arr[2] = 141,...arr[n]
Then, this is my method:
然后,这是我的方法:
public static String split_in_string(int [] an){
int ans;
String ansl = "";
for(int i = 0; i < an.length; i++){
ans = an[i];
ansl = Integer.toString(ans);
return ansl;
}
return ansl;
}
If I use below part in main method:
如果我在 main 方法中使用以下部分:
int ans;
String ansl = "";
for(int i = 0; i < an.length; i++){
ans = an[i];
ansl = Integer.toString(ans);
System.out.println(ansl+" ");
}
It works well.
它运作良好。
But, if I use split_in_string() method, it displays only the first array value.
但是,如果我使用 split_in_string() 方法,它只显示第一个数组值。
I expected 771 11 141 1 11 79 7
as the result, but I get only 771
我预期771 11 141 1 11 79 7
的结果,但我只得到771
What's wrong in my split_in_string() method? How can I fix it?
我的 split_in_string() 方法有什么问题?我该如何解决?
采纳答案by Ziker
Change your method to:
将您的方法更改为:
public static String split_in_string(int [] an){
int ans;
String ansl = "";
for(int i = 0; i < an.length; i++){
ans = an[i];
ansl = Integer.toString(ans);
// return ansl; //commented out this line
Sysytem.out.println(ansl+" "); //added tjis line
}
return ansl;
}
回答by Boolena
try this :
尝试这个 :
public static String split_in_string(int [] an){
int ans;
String ansl = "";
for(int i = 0; i < an.length; i++){
ans = an[i];
ansl += Integer.toString(ans);
//return ans1; //commented this line
}
return ansl;
}
回答by Arjit
Use ansl += Integer.toString(ans);
. You have to append the values to the same string object.
使用ansl += Integer.toString(ans);
. 您必须将值附加到同一个字符串对象。
Try it like this:
像这样尝试:
for(int i = 0; i < an.length; i++){
ans = an[i];
ansl += Integer.toString(ans);
}
回答by laune
You have two return statements in use split_in_string
and so you'll return from the first iteration in the loop.
您有两个 return 语句use split_in_string
,因此您将从循环中的第一次迭代返回。
To return all converted strings, create a List<String>
, add the converted Strings to it, and return that.
要返回所有转换后的字符串,请创建一个List<String>
,将转换后的字符串添加到其中,然后返回。
List<String> split_in_string(int [] an){
List<String> ansl = new ArrayList<String>();
for(int i = 0; i < an.length; i++){
ansl.add( Integer.toString(an[i]) );
}
return ansl;
}
OR, all into a single string:
或者,全部变成一个字符串:
String split_in_string(int [] an){
Stringbuilder ansl = new StringBuilder();
String del = "";
for(int i = 0; i < an.length; i++){
ansl.append( del ).append( Integer.toString(an[i]) );
del = " ";
}
return ansl.toString();
}
回答by Bhesh Gurung
Following also gives you what you want -
以下也给你你想要的 -
String str = Arrays.toString(array).replaceAll(",|\]|\[", "");
e.g.
例如
int[] array = {771, 11, 141, 1, 11, 79, 7};
String str = Arrays.toString(array).replaceAll(",|\]|\[", "");
System.out.println(str);
displays -
显示 -
771 11 141 1 11 79 7
回答by Chemaclass
The problem it's your first return statement
问题是你的第一个 return 语句
for(int i = 0; i < an.length; i++){
ans = an[i];
ansl = Integer.toString(ans);
return ansl; // <- This return doesn't allow to continue the loop foor!
}
return ansl;
However, you can do this for give you want:
但是,您可以根据需要执行此操作:
String str = Arrays.toString(array);
回答by boi yeet
It's best not to use that method. If you have a method that works, it's better to stick with that method than using something else that doesn't work out for you. It's faster and time saving, in my opinion, to use this:
最好不要使用这种方法。如果您有一种有效的方法,那么坚持使用该方法比使用其他对您不起作用的方法要好。在我看来,使用它更快更省时:
int ans;
String ansl = "";
for(int i = 0; i < an.length; i++){
ans = an[i];
ansl = Integer.toString(ans);
System.out.println(ansl+" ");
}
It's what you already have, so it's best to use this method.
这是你已经拥有的,所以最好使用这种方法。