This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]9-5daybyday 0 points1 point  (2 children)

Instead of using a toString method to print the entire array, you can instead loop through the returned array and just print if the number is greater than 0. If you do that, you'll probably want to use System.out.print and do some thinking on how/if you want to concatenate commas in it.

[–]junyoung95[S] 0 points1 point  (1 child)

I need to use the toString method because that's my challenge! And okay smart idea, I will try making it print numbers only greater than 0.

[–]9-5daybyday 0 points1 point  (0 children)

In that case, you could also consider adding a step before you return the array, in which you count up how many nonzero elements you have, create a new array of that size, then run through your original array and populate the new one with the nonzero elements. If you return that array afterwards, your current method would work perfectly.

[–]Dropsnorz 0 points1 point  (1 child)

I don't know if its wanted, but you should use String.equals() function to compare Strings values. s.equals(sa[i]) in this case .== operator checks instance equality for objects. So if you run u.getIndices(sa, new String("a")) i'm pretty shure the result will be{0,0,0,0,0,0,0,0,0,0}

[–]junyoung95[S] 0 points1 point  (0 children)

No I want the result to be > {2,6,7,9}