使用数组列表和字符串比较

时间:2014-11-29 23:02:44

标签: java

我需要从数组列表中读取名称并使用循环比较它们并打印列表中数组的值


import java.io.*;
import java.util.*;

public class UniqueName {

    public static void main(String args[]) throws IOException{
        int wordcount = 0;

        Scanner input = new Scanner(new FileReader("Names.txt"));

        while (input.hasNextLine()) {
        String line = input.nextLine();

           String str [] = line.split((" "));
              for ( int i = 0; i <str.length ; i ++) {
                if (str [i].length() > 0) {
                  wordcount ++; 

                }

                }

        }
        System.out.println(wordcount);
    }
}

2 个答案:

答案 0 :(得分:0)

这是你想要的:

import java.io.*;
import java.util.*;

public class UniqueName {

    public static void main(String args[]) throws IOException{

        Scanner input = new Scanner(new FileReader("Names.txt"));
        Set<String> uniqueNames = new HashSet<String>();

        while (input.hasNextLine()) {
        String line = input.nextLine();

           String str [] = line.split((" "));
              for ( int i = 0; i <str.length ; i ++) {
                if (str [i].length() > 0) {
                  wordcount ++; 

                }
                uniqueNames.add(str[i]);

              }

        }

        System.out.println(wordcount);
        System.out.println(uniqueNames);
    }
}

使用一个集合,它只会添加您传递的值(如果它尚不存在)。 然后你可以用println(uniqueNames)打印出来;这将打印出每个元素,如:“[name1,name2,name3,...,nameN]”

要删除括号和逗号,可以使用:

String str = uniqueNames.toString();
str = str.replace('[', '');
str = str.replace(']', '');
str = str.replace(',', '');

如果你想让每一行都在一个新行上,你可以将replace(',','')改为:replace(',','\ n');

答案 1 :(得分:-1)

在这里,你试着从中学到一些东西:)

public static void main(String args[]) throws IOException {

    Scanner input = new Scanner(new FileReader("names.txt"));
    // this is the arraylist to keep all the names from your file
    ArrayList<String> names = new ArrayList<String>();

    while (input.hasNextLine()) {
        // since some of the names have spaces at the end, we pass them
        // trough a cleanup method to remove the spaces
        String line = clearSpaces(input.nextLine());
        // add the cleaned up names to the arraylist
        names.add(line);
    }

    // loop through all the names in the array for comparisson
    for (int c = 0; c < names.size(); c++) {
        // set each name to be unique until proven otherwise
        boolean unique = true;
        // take the name out of the array to test
        String testString = names.get(c);
        // loop through all the other names in the array for comparisson
        for (int i = 0; i < names.size(); i++) {
            // only if the indexes are different the comparisson makes sense
            if (i != c) {
                // take the name out of the array to test against
                String tempString = names.get(i);
                // test the names against each other
                if (testString.equals(tempString)) {
                    // if they are the same then it's not unique
                    unique = false;
                    // break the loop cause we already know it's not unique
                    break;
                }
            }
        }
        // only if the unique boolean value is still true
        // after testing against all other names
        if (unique)
            // print the name of that unique name
            System.out.println(testString);
    }
}

// returns a string clean of spaces
private static String clearSpaces(String withSpaces) {
    // string builder for the string output
    StringBuilder withoutSpaces = new StringBuilder();
    char[] chars = withSpaces.toCharArray();
    // loop the array of characters
    for (char c : chars) {
        // if it's not equal to 32 which corresponds to space char
        if (c != 32) {
            // append it to the string builder
            withoutSpaces.append(c);
        }
    }
    // return all the chars as string
    return withoutSpaces.toString();
}