how can i remove an array from an array?

How do I remove duplicate names from an array in Java?

  • This is my code. The program takes in names until '-1' is entered and then it prints out the number of names and the names in the order they were entered. import java.util.Scanner; public class NameList { public static void main(String [] args) { String firstName; int count = 0; int nameSize = 0; String[]name = new String [20]; Scanner reader = new Scanner(System.in); System.out.print("Please enter a name: "); firstName = reader.nextLine(); while(!(firstName.equals("-1"))&&(na… < 20)) { name[nameSize] = firstName; nameSize = nameSize + 1; System.out.print("Please enter a name: "); firstName = reader.nextLine(); count++; } System.out.println(); System.out.println("The total number of names is: "+count); for(int index = 0;index < nameSize; index++) { System.out.print(name[index]+" "); } } } If one name is entered twice, how can I get the program to delete the duplicate entry?

  • Answer:

    The easiest way is to eliminate duplicates before they get in the array to begin with...after getting each name, scan thru the array to see if it's already there and, if not, add it.

Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

if u don't want any duplicates, then an array won't help u... u should use a Set... if u want ur Set sorted, you can use a TreeSet... otherwise just a HashSet would do... using these arent complicated... all u need to know is there in the java api... just check it out and look for TreeSet and HashSet under the classes list..and also, these are in the util package.. so import these before tryna use them... http://download.oracle.com/javase/6/docs/api/

Agree with the first guy. What you want is a Set, which is just an Array but without duplicate entries. Just use a HashSet: http://www.java2s.com/Tutorial/Java/0140__Collections/SetandHashSet.htm

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.