0
Getting No Output. Can anyone say why?
class Myclass { public static void main(String[] args) { String [] arr = {"a" , "b" , "c"}; String a="abc"; String [] part = a.split(""); for(int i=0;i<part.length;i++) { if(arr[i]==part[i]) { System.out.print(part[i]); } } } }
2 Réponses
+ 2
Because if(arr[i] == part[i]) is false on every line. Change it to if(arr[i].equals(part[i])) and it should work.
Always use equals() method to compare Strings (and other objects too). Because (arr[i] == part[i]) checks if it is exactly the same object
0
You are using wrong comparator == is for object reference. Use equals method to compare content arr[i].equals(part[i])