Substring alternative

I have a strange problem when adding a value to a String array, which later is involved in sorting the array using a hash map. I have an XFR900a file name, and part of XFR900 is added to the array using the following code:

private ArrayList<String> Types = new ArrayList<String>(); ... Types.add(name.substring(0,(name.length() - 1)); System.out.println(name.substring(0,(name.length() - 1)); 

I even print a line that gives "XFR900", however subsequently sorting the array behaves differently when I use the following code:

 Types.add("XFR900"); System.out.println(name.substring(0,(name.length() - 1)); 

which is just a part of a substring made by hand, very confusing.

Are there any good alternatives to substring since there should be some odd character without ascii?

Phil

UPDATE

Thanks for your comments. Here are some of the code that compares the string later;

  for (int i=0;i< matchedArray.size();i++){ //run through the arrays if (last == matchedArray.get(i)) { //add arrays to a data array ArrayList data = new ArrayList(); data.add(matchedArray1.get(i)); data.add(matchedArray2.get(i)); data.add(matchedArray3.get(i)); data.add(matchedArray4.get(i)); data.add(matchedArray5.get(i)); //put into hash map map.put(matchedArray.get(i), data); } else { //TODO System.out.println("DO NOT MATCH :" + last + "-" + matchedArray.get(i)); 

As you can see, I added the System.out.println test ("DO NOT SHOULD" ... and below - this is some kind of conclusion;

DON'T FOLLOW: FR99-XFR900 DON'T CONNECT: XFR900-XFR900

I run a substring for the XFR900a file name only. The problem is that for the test string to be printed last! = MatchedArray.get (i), however they will be the same when printed on the display.

Phil

+4
source share
3 answers

You should not use the == operator to compare the contents of strings. == checks if it is the same object. Instead, write last.equals(matchedArray.get(i)) . The equals() method checks if the object is equal if they are the same. In the case of String it checks to see if two strings consist of the same characters. This can eliminate your strange behavior.

PS: the behavior of == in a string is a little unpredictable because the java virtual machine does some optimization. If the two lines are equal, it is possible that jvm uses the same object for both. This is possible because String objects are immutable. This explains the difference in behavior if you write the substring manually. In one case, jvm is optimized, and in the other, not.

+3
source

Use .equals() , not == , because these are strings!

 if (last.equals(matchedArray.get(i))) {} 
0
source

Never use the == operator, if you want to check the value, since the operator will check the equality of the reference to the object, use the equality operator, which checks the value, and not the ie link

 for (int i=0;i< matchedArray.size();i++){ //run through the arrays if (last.equals(matchedArray.get(i))) { // Line edited //add arrays to a data array ArrayList data = new ArrayList(); data.add(matchedArray1.get(i)); data.add(matchedArray2.get(i)); data.add(matchedArray3.get(i)); data.add(matchedArray4.get(i)); data.add(matchedArray5.get(i)); //put into hash map map.put(matchedArray.get(i), data); } else { //TODO System.out.println("DO NOT MATCH :" + last + "-" + matchedArray.get(i)); 
0
source

All Articles