我很新的Java,但我想應該能夠很容易地聚集在接下來的輸入(兩列):如何聚合字符串並在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中做到這一點?
謝謝。
我很新的Java,但我想應該能夠很容易地聚集在接下來的輸入(兩列):如何聚合字符串並在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中做到這一點?
謝謝。
1讀出的線
2使用分割來分割的每一行,並獲得第一和第二元件
3把它們放在一個地圖>:搜索在什麼
4迭代和什麼展示你的結果
有很多方法可以用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); }
}
}
}
我希望這可以幫助你一點。
你可以使用一些東西一樣的HashMap –
Abdelhak
純代碼編寫請求是題外話堆棧溢出 - 我們期望在這裏 問題涉及到具體* *編程問題 - 但我們 會很樂意幫你寫吧你自己!告訴我們 [你試過的東西](http://whathaveyoutried.com),以及你卡在哪裏。 這也將幫助我們更好地回答你的問題。 –
@ElliottFrisch我猜我一開始就卡住了。有很多關於如何將字符串拆分爲我的輸入格式的信息,但沒有太多如何聚合/連接成字符串。我試過了StringUtils.join(column2,「,」),但是如果我想爲第1列中的每個單詞進行聚合,它不起作用。我猜測某種循環可能會起作用,但不幸的是,我在Java中的技能是不是很好。 – simtim