Reputation: 796
I have a String array that contains the "_" characheter in each element of it, I want to get rid of these characters.
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.
I have write a code to do this task but the output are strange characters [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) );
}
}
}
Upvotes: 2
Views: 5586
Reputation: 1
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]+"");
}
}
}
}
Upvotes: 0
Reputation: 1315
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;
}
Upvotes: 3
Reputation: 95948
In Java, each object has toString()
method, the default is displaying the class name representation, then adding @
and then the hashcode.
ss
is an array of String
s. You should use Arrays#toString()
, which is implemented this way:
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);
}
Upvotes: 3
Reputation: 691635
What you see there is the result of the toString()
method when invoked on arrays. It's almost meaningless. What is printed is the type of the array followed by its hashCode.
Use java.util.Arrays.toString()
to transform an array into a meaningful String representation.
Upvotes: 0