java - string array contain duplicate value and remove and print the array -
this question has answer here:
- how compare strings in java? 23 answers
sorry ask question again had found alot in forum.but of new java excepting explanation code tried in way not working .i tried without default method code have work program
code:
package javatest; public class test03sep { public static void main(string args[]) { //0 1 2 3 string []val={"amma","senthil","kumar","amma"}; removeduplicate(val); } static void removeduplicate(final string []arr) { string temp=""; (int = 0; < arr.length; i++) { (int j = i+1; j < arr.length; j++) { system.out.print("condition are:"); system.out.println(arr[i]==arr[j]); if(arr[i]==arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } (string string : arr) { system.out.println("string array are==>"+string); } } } }
output:
condition are:false condition are:false condition are:true string array are==>amma string array are==>senthil string array are==>kumar string array are==>amma condition are:false condition are:false string array are==>amma string array are==>senthil string array are==>kumar string array are==>amma condition are:false string array are==>amma string array are==>senthil string array are==>kumar string array are==>amma string array are==>amma string array are==>senthil string array are==>kumar string array are==>amma
but excepting output string array are===>{"amma","senthil","kumar"};
could 1 can guide on please????
please change if(arr[i]==arr[j])
if(arr[i].equals(arr[j]))
while comparing string objects should use equals()
method instead of "=="
Comments
Post a Comment