我是Java新手,但我希望能够轻松汇总以下输入(两列):
one a
one b
two a
two c
three a
three b
three c
进入以下输出(两列):
one a,b
two a,c
three a,b,c
因此,对于第一列中的每个单词,我想获得逗号分隔单词列表。 有没有简单的方法可以用Java做到这一点?
谢谢。
答案 0 :(得分:1)
使用Java完成此任务有很多方法,但我将向您展示如何使用带有小型正则表达式字符串的模式匹配器来完成此任务。
下面的代码可能看起来很大,但是如果删除所有评论,你会发现它很小。这个工作马在名为SetAggregateList()的方法中,正如我之前提到的那样,它被广泛评论,以便你知道做了什么。只需加载并运行它,然后查看输出控制台(面板)以获取最终列表结果。
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author DevilsHnd
*/
public class AggregateList {
public static void main(String[] args) {
/* This string array contains our so called two word strings. How these
strings get into the array is up to you. */
String[] sArray = {"one a","one b","two a","two c","three a","three b","three c"};
/* Declare and establish an ArrayList variable. This ArrayList will
ultimately hold our aggregate strings. */
ArrayList<String> aggregateStrings = new ArrayList<>();
//Okay...let's fire things up and fill our ArrayList with
// aggregate strings.
SetAggregateList(aggregateStrings, sArray);
// Now that we've filled out ArrayList let's iterate through
// it and display the contents of each list element.
for (int i = 0; i < aggregateStrings.size(); i++) {
System.out.println(aggregateStrings.get(i));
}
}
private static void SetAggregateList(ArrayList<String> arrayList, String[] sArray) {
/* We start by iterating (looping) through our two word strings held
within the sArray[] string array. */
for (int i = 0; i < sArray.length; i++) {
/* We place the array element into a string variable
named element so that we don't need to reference
the index all the time. */
String element = sArray[i];
/* We establish yet two more string variables so as
to hold each word contained within each array element
as we iterate through the array. */
String word1 = "", word2 = "";
/* To fill the word1 and word2 string variables we're
going to use the Pattern Matcher method and a small
regex expression for obtaining the '2nd' word and
beyond.
Here we establish our pattern to use and place it into
a variable conveniently named pattern.... */
Pattern pattern = Pattern.compile("(?i)\\s([a-z]+)");
/* Breakdown of the expression string above: "(?i)\\s([a-z]+)"
(?i) ignore letter case.
\\s start from the first space encountered.
( group start...
[a-z] accept only letters from a to z (or A to Z because we ignore
case) after the space that was encountered.
+ Match one or more of the previous item which in this case is
the [a-z].
) group end. Group start & group end establishes what is called
Group 1. */
/* We now run the pattern through the matcher method to
see if there is a match to our regex expression. */
Matcher matcher = pattern.matcher(element);
// See if the matcher method finds a match to our expression.
if (matcher.find()) {
/* it does so place that group string into our word2 variable
but trim any leading or trailing spaces from it first. */
word2 = matcher.group(1).trim();
/* Now that we have the second section of the string contained
in the array element we can easily extract the first section
by removing the space and 2nd string section from the array
element. And to make sure there are no leading or trailing
spaces, we trim it as well. */
word1 = element.replace(" " + word2,"").trim();
}
/* This boolean variable is simply used as a flag to inform the iteration
process that the first word in a array element we are processing is
already contained within our aggregateStrings ArrayList. This way we
ensure singularity (no duplicates). */
boolean haveIt = false;
/* Here is the crunch of it all. We now need to iterate through our
ArrayList we've created to see if we have already placed the first
word of the string Array element we are currently processing into
the list. If it isn't then we add it but if it is then we need to
check the second word (string section) and compare it to the second
word from our string array element and see if that is also already
contained within the list for that first word. If it isn't then we
add it to the first word but first applying a comma to delimit it
from any other 2nd words we have previously added. */
// Iterate through the aggregateStrings ArrayList...
for (int j = 0; j < arrayList.size(); j++) {
/* We place the ArrayList element into a string variable
named aElement so that we don't need to reference
the index all the time. */
String aElement = arrayList.get(j);
/* We establish yet two more string variables so as to
hold each word contained within each ArrayList element
as we iterate through the ArrayList. */
String alWord1 = "", alWord2 = "";
/* To fill the alWord1 and alWord2 string variables we're
going to use the Pattern Matcher method yet again and a
small regex expression for obtaining the '2nd' word and
beyond. This expression is a little different than the
one we used earlier, if you look closely you can see that
we have added a comma (,) in our matches section of the
expression [a-z,]. We need to do this because as we are
developing our aggregate strings we are adding commas for
delimiters between all our second words. We want to accept
these during our Pattern Match for separating the two
string sections for comparisons.
Here we establish our pattern to use and place it into
a variable named aPattern.... */
Pattern aPattern = Pattern.compile("(?i)\\s([a-z,]+)");
/* Breakdown of the expression string above: "(?i)\\s([a-z]+)"
(?i) ignore letter case.
\\s start from the first space encountered.
( group start...
[a-z,] accept only letters from a to z (or A to Z because we ignore
case) and commas after the space that was encountered.
+ Match one or more of the previous item which in this case is
the [a-z,].
) group end. Group start & group end establishes what is called
Group 1. */
/* We now run the pattern through the matcher method to
see if there is a match to our regex expression. */
Matcher aMatcher = aPattern.matcher(aElement);
// See if the matcher method finds a match to our expression.
if (aMatcher.find()) {
/* it does so place that group string into our alWord2 variable
but trim any leading or trailing spaces from it first. */
alWord2 = aMatcher.group(1).trim();
/* Now that we have the second section of the string contained
in the ArrayList element we can easily extract the first section
by removing the space and 2nd string section from the ArrayList
element. And to make sure there are no leading or trailing
spaces, we trim it as well. */
alWord1 = aElement.replace(" " + alWord2,"").trim();
}
/* Here we do our checks. First we compare the first word from our
ArrayList iteration element against the fist word of the string
Array iteration... */
if (alWord1.equals(word1)) {
/* If it's a match then we know that the first word is already
contained within the ArrayList so we set our haveIt flag to
true so as to ensure the process doesn't add it again. */
haveIt = true;
/* Now we need to see if the second word section of the ArrayList
already contains the second word from the string Array element
currently in process. */
if (!alWord2.contains(word2)) {
// It doesn't so add it to the ArrayList element.
aElement+= "," + word2;
arrayList.set(j, aElement);
}
}
}
/* Here we check to see if our haveIt flag is false and if it is then this
obviously means that we do not have the String Array element first word
within our ArrayList so we add it. */
if (!haveIt) { arrayList.add(element); }
}
}
}
我希望这能帮到你一点点。
答案 1 :(得分:0)
1阅读
行2使用split来分割每一行,并获得第一和第二个元素
3将它们放入地图&gt; :搜索什么
4迭代并显示您的结果