[英]Splitting String array without using split(String regex) method
I have a String array that contains the "_" characheter in each element of it, I want to get rid of these characters. 我有一个String数组,它的每个元素中都包含“ _”字符,我想摆脱这些字符。
I can do this task simply by using String [] split(String regex) method, but I don't want to it by this function because I will use this code in J2ME later. 我可以简单地通过使用String [] split(String regex)方法来完成此任务,但是我不希望通过此函数来执行此任务,因为稍后将在J2ME中使用此代码。
I have write a code to do this task but the output are strange characters [Ljava.lang.String;@19821f [Ljava.lang.String;@addbf1 !!! 我已经编写了代码来执行此任务,但输出是奇怪的字符[Ljava.lang.String; @ 19821f [Ljava.lang.String; @ addbf1 !!!
public class StringFragementation {
static public String [] mymethod(String [] mystring)
{
String ss [] =new String[mystring.length];
for(int j=0;j<mystring.length;j++)
{
ss[j] = mystring[j].replace('_',',');
}
return ss ;
}
public static void main(String [] args)
{
String [] s = {"Netherlands_Iceland_Norway_Denmark","Usa_Brazil_Argentina"};
for(int i=0;i<s.length;i++)
{
System.out.println("" + mymethod(s) );
}
}
}
In Java, each object has toString()
method, the default is displaying the class name representation, then adding @
and then the hashcode. 在Java中,每个对象都有
toString()
方法,默认是显示类名称表示形式,然后添加@
,然后添加哈希码。
ss
is an array of String
s. ss
是String
的数组 。 You should use Arrays#toString()
, which is implemented this way : 您应该使用
Arrays#toString()
,它是通过以下方式实现的:
3860 public static String toString(int[] a) { {
3861 if (a == null)
3862 return "null";
3863 int iMax = a.length - 1;
3864 if (iMax == -1)
3865 return "[]";
3866
3867 StringBuilder b = new StringBuilder();
3868 b.append('[');
3869 for (int i = 0; ; i++) {
3870 b.append(a[i]);
3871 if (i == iMax)
3872 return b.append(']').toString();
3873 b.append(", ");
3874 }
3875 }
Or, you can do: 或者,您可以执行以下操作:
for(String str : mymethod(s)) {
System.out.println(str);
}
I have modified your code. 我已经修改了您的代码。 It will give you desired output.
它将为您提供所需的输出。 use following code
使用以下代码
public static void main(String[] args) {
String[] s = {"Netherlands_Iceland_Norway_Denmark", "Usa_Brazil_Argentina"};
String[] finalString = mymethod(s);
for (int i = 0; i < s.length; i++) {
System.out.println("" + finalString[i]);
}
}
static public String[] mymethod(String[] mystring) {
String ss[] = new String[mystring.length];
for (int j = 0; j < mystring.length; j++) {
ss[j] = mystring[j].replace('_', ',');
}
return ss;
}
What you see there is the result of the toString()
method when invoked on arrays. 您所看到的是在数组上调用
toString()
方法的结果。 It's almost meaningless. 这几乎没有意义。 What is printed is the type of the array followed by its hashCode.
打印的是数组的类型,后跟其hashCode。
Use java.util.Arrays.toString()
to transform an array into a meaningful String representation. 使用
java.util.Arrays.toString()
将数组转换为有意义的String表示形式。
public class MyDisticntElements {
public static void main(String[] args)
{
int [] nums = {2,3,4,5,8,7,7};
MyDisticntElements.printDuplicate(nums);
}
public static void printDuplicate(int[] arr)
{
for(int i =0; i<arr.length; i++)
{
boolean duplicate = false;
for(int j = 0; j<i; j++)
{
if(arr[i] == arr[j])
{
duplicate = true;
break;
}
}
if(duplicate)
{
System.out.println(arr[i]+"");
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.