开发者

Java ArrayList problem (involves valueOf())

So I have this function, combinations, which adds to an arraylist all permutations of a string.

 public static void combinations(String prefix, String s, ArrayList PermAttr) {
        if (s.length() > 0) {
            PermAttr.add(prefix + s.valueOf(s.charAt(0)));
            combinations(prefix + s.valueOf(s.charAt(0)), s.substring(1), PermAttr);
            combinations(prefix,       开发者_高级运维        s.substring(1), PermAttr);
        }
 }  

Now, I have this arrayList tryCK which let us say is {"A","B"}.

I have another arrayList CK which is also {"A","B"}, but it was derived from the combinations function above.

When I do tryCK.equals(CK) it returns true.

But when I put it through a another function I have on both tryCK and CK, for tryCK it returns true and CK it returns false, even though they are exactly the same lists.

So, my question is, does using .valueOf(s.charAt()) change some inner type?

This is super hard to explain but I do not want to post the full code.


First of All, you don't even really need to use valueOf because most always, Java is just fine concatenizing characters to the end of strings.

 PermAttr.add(prefix + s.charAt(0));

Second, If you are going to use valueOf, at least reference it from the class String, not an instance of a String object...

PermAttr.add(prefix + String.valueOf(s.charAt(0)));

Third, a better naming convention would be helpful:

permAttr.add(prefix + s.valueOf(s.charAt(0)));

Fourth, there are plenty of ways to check the contents of your ArrayList, try making sure they actually contain the same values instead of assuming they do:

for(String s : CK)
 System.out.println(s);

Fifth, you summed up with "So, my question is, does using .valueOf(s.charAt()) change some inner type?" and the answer is:

valueOf(s.charAt(int)) will return a String object. If you were not anticipating a string, then yes, this changes the type of the object. This String functions normally with all other Strings (like the ones you are concatenizing it with) and should do anything a String can do. So if you were expecting a String type, then no, it does not change any type.

Sixth, make sure if you are comparing strings to use the equals() method.

DO NOT USE:

if(s1 == s2)

this will check to see if the reference the same location in memory.

USE

if(s1.equals(s2))

this will check to see if the values of the Strings are the same.

I think that's all I got. GOOD LUCK!


First issue: you're using String.valueOf as if it were an instance method, when it's actually a static method. That leads to very misleading code.

Second issue: you don't have a consistent naming convention. Parameters should generally be camel cased - naming a parameter PermAttr leads to method calls such as:

PermAttr.add(prefix + s.valueOf(s.charAt(0)));

which looks like a static method call in a class called PermAttr.

Third issue: you're using String.valueOf for no reason - you're already using string concatenation, so using just:

prefix + s.charAt(0)

would be fine.

None of those are actually responsible for whatever's wrong (which we can't easily tell without a short but complete example demonstrating the problem) but they are making it harder to understand the code.

I suggest you fix the above issues, and put this into the context of a short but complete program which does demonstrate the problem. It should then be reasonably easy to work out what's wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜