so trying sort words alphabetically using linear sort , delete word if there word it. used following method:
import java.util.arrays; public class sorting { public static void main(string[] args) { string[] array = new string[] { "pepperoni", "ham", "bacon", "pineapple", "ham", "sausage", "onion", "bacon" }; system.out.println("before sorting: " + arrays.tostring(array)); (int = 0; < array.length; i++) { int min = i; (int j = i; j < array.length; j++) { if (array[min].compareto(array[j]) > 0) { min = j; } else if (array[min].equals(array[j]) == true) { array[j] = ""; } } string tmp = array[i]; array[i] = array[min]; array[min] = tmp; } system.out.println("after sorting: " + arrays.tostring(array)); } } but deleted. without else if statement sorted out, deleted.
before sorting: [pepperoni, ham, bacon, pineapple, ham, sausage, onion, bacon] after sorting: [, , , , , , , ] can point out what's wrong code?
i think 2 should be:
for(i = 0; < array.length - 1; i++) and
for(j = + 1; j < array.length; j++) so sure i , j different. in implementation in fact, compare element element itself, , (wrongly) assume it's duplicate.
moreover can use:
if(array[min] < array[j]) and
else if(array[min].equals(array[j]))
Comments
Post a Comment