0

I have an array list with values such as

{ "november", "a","b","c","d", "december", "i","j","k", "april", "g","h" }

What is the best way to split it so i can have different arrays based on months.for example

List<String> novemberArray={"a","b","c","d"}

and

List<String> decemberArray={"i","j","k"}` 

etc..

I am getting this list from parsing an html page with Jsoup.

Elements tableRowElements = tableElements.select(":not(thead) tr");
for (int i = 0; i < tableRowElements.size(); i++) {
  Element row = tableRowElements.get(i);
  System.out.println("row : " );
  Elements rowItems = row.select("tr");
  for (int j = 1; j < rowItems.size(); j++) {
    System.out.println(rowItems.get(j).text());
    myList.add(rowItems.get(j).text());
  }
}

myList has the values of { "november", "a","b","c","d", "december", "i","j","k", "april", "g","h" } List is dynamic and i dont have any control on the positions in the list. Using sub indexes will not help because they change frequently.

4

4 回答 4

1

一种方法是 找到开始和结束索引并使用subList(int fromIndex, int toIndex)来获取原始列表的一部分的视图。

从 API:

返回此列表在指定的 fromIndex(包括)和 toIndex(不包括)之间的部分的视图。(如果 fromIndex 和 toIndex 相等,则返回列表为空。)返回列表由此列表支持,因此返回列表中的非结构性更改会反映在此列表中,反之亦然。返回的列表支持此列表支持的所有可选列表操作。

于 2013-11-07T17:29:26.667 回答
1

所以你有一个ArrayList包含数据("a","b")和哨兵值(等)?Orel Eraki 的评论很到位,您在处理这样的数据时遇到了麻烦。如果可以,您会发现完全避免这种方法的痛苦要少得多。"november"

但是,假设你无法改变它,这就是你可以摆脱它的方法。我使用Guava Multimap,我强烈建议你也这样做,但你可以用HashMap<String, ArrayList<String>>一个额外的样板替换 a 。

public ListMultimap<String,String> removeSentinels(List<String> ls,
                                                   Set<String> sentinels) {
  String currentSentinel = null;
  ArrayListMultimap<String,String> map = ArrayListMultimap.create();
  for(String s : ls) {
    if(sentinels.contains(s)) {
      currentSentinel = s;
    } else {
      /*
      Note if the list doesn't start with a sentinel value, it will put items in the
      null entry.  You could instead do a null check here and raise an exception, or
      use an ImmutableListMultimap which forbids null keys or values.
      */
      map.put(currentSentinel, s);
    }
  }
  return map;
}

然后,您可以使用List<String> novemberList = map.get("november");等获取元素。


您的 JSoup 示例解释了潜在的概念问题。您正在获取 2D 表格数据并将其读入 1D 列表。我在上面描述的相同行为可用于从一开始就更干净地将此表数据解析为 a Multimap,避免以后重新解析的需要(或者,Guava 也提供了一个Table接口,但这对于您的用例来说可能是多余的):

public ListMultimap<String,String> tableToMap(Document doc) {
  Elements trElems = doc.select(":not(thead) tr");
  ArrayListMultimap<String,String> map = ArrayListMultimap.create();
  for(Element tr : trElems) {
    // I assume you meant td, a <tr> shouldn't contain <tr>'s
    Elements tdElems = tr.select("td");
    String month = tdElems.get(1).text(); // You skip index 0, presumably intentionally
    for(int i = 2; i < tdElems.size(); i++) {
      map.put(month, tdElems.get(i).text());
    }
  }
  return map;
}
于 2013-11-07T17:51:25.147 回答
0

创建一个字符串到列表的映射,以将月份作为键和 a、b、c 等列表作为值。

遍历 ArrayList 并对照一个列表或一组月份字符串(“January”,“February”,...)检查每个条目,每次遇到肯定匹配时,然后将“currentMonth”变量设置为匹配月份并添加/为以下每个非月变量更新 Map 中的相应列表条目。

伪代码:

If (entry is month) {
    currentMonth = entry;
} else {
    List entries = monthMap.get(currentMonth) == null ? new ArrayList<>() : monthMap.get(currentMonth)
    entries.add(entry)
    monthMap.put(currentMonth, entries)
}
于 2013-11-07T17:38:02.670 回答
-1

这是外行的方法

import java.util.*;

public class One {
    public static void main(String[] args) 
    {
        String[] inputArray = new String[]{ "november", "a","b","c","d","december", "i","j","k","april", "g","h"};
        List<String> monthNames = new ArrayList<String>();
        List<String> monthNamesOutput = new ArrayList<String>();
        List<String> alphabetsOutput = new ArrayList<String>();
        monthNames.add("january");
        monthNames.add("february");
        monthNames.add("march");
        monthNames.add("april");
        monthNames.add("may");
        monthNames.add("june");
        monthNames.add("july");
        monthNames.add("august");
        monthNames.add("september");
        monthNames.add("october");
        monthNames.add("november");
        monthNames.add("december");
        for(String temp:inputArray)
        {
            if(monthNames.contains(temp))
            {
                monthNamesOutput.add(temp);
            }
            else
            {
                alphabetsOutput.add(temp);
            }
        }
        System.out.println(monthNamesOutput);
        System.out.println(alphabetsOutput);
    }
}
于 2013-11-07T17:52:38.503 回答