打印ArrayList,同时读取它以获取打印条件

时间:2013-03-14 04:25:20

标签: java loops printing arraylist anagram

我有两个ArrayList<String>个。一个包含带标点和大写的单词,另一个包含删除的单词。

的List1:

File word: it's
File word: Sit
File word: yes-man
File word: murdered
File word: ok
File word: DereDrum
File word: Hello
File word: Friend

列表2:

Edited word: its
Edited word: sit
Edited word: yesman
Edited word: murdered
Edited word: ok
Edited word: deredrum
Edited word: hello
Edited word: friend

这个想法是检查并查看哪些词是彼此的字谜。如果是这样的话,我想要做的就是重新印刷它们,但是在同一条线上的字样是彼此相同的:

it's Sit

yes-man

murdered DereDrum

ok

hello

friend

我试着这样做:

        for (int i = 0; i < words.size(); i++)
        {
            System.out.print(words.get(i));
            for (int j = i+1; j < grams.size(); j++)
            {
                anagramChecker(grams.get(i),"",grams.get(j),words.get(j));
            }
            System.out.println();
        }

我想到的方式是这个for循环持续List1(单词)的大小并打印它正在看的单词。然后我用我的anagramChecker:

public static void anagramChecker(String s1,String s2,String checked,String word)
{
    if(s1.length() == 0)
    {
        if(s2.equals(checked))
        {
            System.out.println(" " + word);
            return;
        }
    }
    for(int i = 0 ; i < s1.length() ; i++)
    {
        anagramChecker(s1.substring(0, i) + s1.substring(i+1, s1.length()),s2 + s1.charAt(i),checked,word);
    }
}

这种方法不起作用,它的输出是这样的:

it's
Sit yes-man
 yes-man
 yes-man
 yes-man
 yes-man
 yes-man
 yes-man
 yes-man

yes-man
murdered
ok
DereDrum Hello

Hello
Friend

我尝试使用地图,因此我可以按字母顺序对单词进行排序,然后在第二个列表中按字母顺序对单词进行排序。这看起来大致相同。

3 个答案:

答案 0 :(得分:2)

重新使用here中的anagram逻辑。

public boolean areAnagrams(String s1, String s2) {
    //TODO : Strip punctuations from s1 and s2
    char[] ch1 = s1.toCharArray();
    char[] ch2 = s2.toCharArray();
    Arrays.sort(ch1);
    Arrays.sort(ch2);
    return Arrays.equals(ch1,ch2);
}

遍历两个列表并检查areAnagrams是否为真,然后打印出来。

for (int i=0;i<size;i++) {  
    String first = firstList.get(i);
    String second = secondList.get(i);
    if (areAnagrams (first, second) {
        System.out.println(first + " " + second);
    }    
}

答案 1 :(得分:2)

尝试以下代码。

import java.util.ArrayList;
import java.util.List;

public class Client {

    public static void main(String[] args) {

        List<String> lst1 = new ArrayList<String>();
        List<String> lst2 = new ArrayList<String>();

        lst1.add("it's");
        lst1.add("Sit");
        lst1.add("yes-man");
        lst1.add("murdered");
        lst1.add("ok");
        lst1.add("DereDrum");
        lst1.add("Hello");
        lst1.add("Friend");

        lst2.add("its");
        lst2.add("sit");
        lst2.add("yesman");
        lst2.add("murdered");
        lst2.add("ok");
        lst2.add("deredrum");
        lst2.add("hello");
        lst2.add("friend");

        for (int i = 0; i < lst2.size(); i++) {
            String original = lst2.get(i);
            StringBuffer buff = new StringBuffer(original);
            buff.reverse();
            if (lst2.contains(buff.toString())) {
                System.out.println(lst1.get(i) + " "
                        + lst1.get(lst2.indexOf(buff.toString())));
                lst1.remove(lst2.indexOf(buff.toString()));
                lst2.remove(buff.toString());
            } else {
                System.out.println(lst1.get(i));
            }

        }

    }

}

输出将是。

it's
Sit
yes-man
murdered DereDrum
ok
Hello
Friend

您可以点击以下链接在线查看。

Example Java Online Running Fiddle

答案 2 :(得分:1)

再做一遍......其他人已经告诉过你关键点了。 把它放到char数组中,对它进行排序,比较,完成。

package array;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Anagrams {

    private static ArrayList<Character> convert(String txt) {
    ArrayList<Character> alph = new ArrayList<Character>();
    for (int i = 0; i < txt.length(); i++)
        if (Character.isLetter(txt.charAt(i)))
        alph.add(Character.toLowerCase(txt.charAt(i)));
    Collections.sort(alph);
    return alph;
    }

    public static void main(String[] args) {
    ArrayList<String> first = new ArrayList<String>();
    first.add("it's");
    first.add("Sit");
    first.add("yes-man");
    first.add("murdered");
    first.add("ok");
    first.add("DereDrum");
    first.add("Hello");
    first.add("Friend");

    ArrayList<String> second = new ArrayList<String>();
    second.add("its");
    second.add("sit");
    second.add("yesman");
    second.add("murdered");
    second.add("ok");
    second.add("deredrum");
    second.add("hello");
    second.add("friend");

    ArrayList<Integer> checked = new ArrayList<Integer>();

    for (int i = 0; i < first.size(); i++) {
        String line = "";
        if (!checked.contains(i))
        line += first.get(i) + " ";

        ArrayList<Character> firstChrAry = convert(first.get(i));

        for (int j = i + 1; j < second.size(); j++) {
        ArrayList<Character> secondChrAry = convert(second.get(j));
        if (firstChrAry.size() == secondChrAry.size()) {
            if (firstChrAry.equals(secondChrAry)) {
            line += first.get(j);
            checked.add(j);
            }
        }
        }
        if (!line.equals(""))
        System.out.println(line);
    }

    }
}

在此处添加我的输出结果:

它坐着

是的 - 男人

谋杀了DereDrum

好的

你好

朋友