提取名称的ArrayList减去名字

时间:2014-04-13 01:01:30

标签: java arrays

阅读

等字符串列表
Bob,Jim,Greg
Bob,Allison,Jamie,Craig 
Bob,Tim,Allen

如何使用

获取ArrayList
Jim Greg as the first element
Allison Jamie Craig as the second element 
Tim Allen as the third element

4 个答案:

答案 0 :(得分:1)

List<String> list = new ArrayList<String>(stringLinks.size());
String line;
while((line = bufferedReader.readLine()) != null) {
    String[] names = line.split(",")
    StringBuilder tmp = new StringBuilder(names[1]);
    for(int i = 2; i < names.length; i++) {
        tmp.append(" ").append(name);
    }
    list.add(tmp.toString());
}

答案 1 :(得分:0)

假设上面的字符串是分开的并且在某种类型的集合中可用(我假设一个数组),你只需要对逗号字符进行拆分然后追加剩下的部分。 (这是一种非常简单的技术,但您可以使用正则表达式来提取更复杂的结构)。

public List extractNames(String[] nameArr)
{
     List<Steing> nameList = new ArrayList<String>():
     for(String str : nameArr)
     {
         String[] tokens = name.split(",");
         String name = "";
         for(int i = 1 ; i < tokens.length ; ++i)
         {
               name += tokens[i];
         }
         nameList.add(name);
     }
  return nameList;
}

答案 2 :(得分:0)

ArrayList = x; // initializated before.
for (int i=x.size(); i==0; i--){
   if(i!=0){
      // extract the elements of x and store in another arraylist
   }
   // if i==0 simple do nothing.
}

答案 3 :(得分:0)

String line = null; 
StringTokenizer tizer = null;
ArrayList<String> nameList = new ArrayList<String>();
while((line = in.readLine())!=null)
{
    tizer = new StringTokenizer(line,",");
    tizer.nextToken(); //Get rid of Bob
    String tempStr = "";
    while(tizer.hasMoreTokens())
    {
        tempStr = tempStr + tizer.nextToken() + " "; //Get the rest of the names per line
    }
    nameList.add(tempStr.trim());
}