Reputation: 1
I'm unsure of how to return an array of strings. Originally, I populated the array with a for loop then just converted it to a string using toString(). I am not allowed to change the return type so I would like to know how do you return the string array? I tried returning the array, but it returned the memory address. Here's the code:
public String getValues(int start, int end)
{
String[] values = new String[end+1];
for(int i=0;i<values.length;i++)
{
if(i % ham != 0 && i % spam != 0)
{
values[i]=Integer.toString(i);
}
else if(i % ham == 0 && i % spam != 0)
{
values[i]= word2;
}
else if(i % ham != 0 && i % spam == 0)
{
values[i]= word3;
}
else if(i % ham == 0 && i % spam == 0)
{
values[i]= word1;
}
}
String[] result= java.util.Arrays.copyOfRange(values,2,end+1);
String str1= java.util.Arrays.toString(result);
return str1;
}
I am only allowed to edit this class. My method has to return the String array with all of the elements in it. No println, print, or anything of that sort. I thought that just returning the array would work, but I just get the address. I don't know what the problem is.I changed the return type to String[] and took out the toString() and just returned result. Still getting the address of the array, to be specific this: [Ljava.lang.String;@391673
Upvotes: 0
Views: 1511
Reputation: 156404
String[]
.public String[] getValues(int start, int end) {
// ...
return result;
}
Then you can print the results as such:
public static void main(String[] args) {
String[] values = new MyClass().getValues(1, 2);
String valuesString = java.util.Arrays.toString(values);
System.out.println("VALUES: " + valuesString);
}
Upvotes: 3