【问题标题】:Store indexes in an array将索引存储在数组中
【发布时间】:2011-04-04 20:10:15
【问题描述】:

我需要有关如何存储句子中出现的特定单词的索引的帮助。 我需要将索引存储在一个数组中,以便以后可以访问它。我正在使用 while 循环,但它不起作用。

while (index > 0) {

            for (int i = 0; i < data.length; i++) {

                data[i] = index;

            }

            System.out.println("Index : " + index);


            index = input.indexOf(word, index + word.length());

        }

【问题讨论】:

  • 您能否详细说明您正在尝试完成什么、您希望数据保存什么以及初始化为什么索引?

标签: java arrays string


【解决方案1】:

我在下面评论了您的代码。请阅读 cmets 了解。

while (index > 0) { //String.indexOf can return a 0 as a valid answer. Use -1.
//Looping over something... Why don't you show us the primer code?
    for (int i = 0; i < data.length; i++) {
        /*
        Looping over the `data` array.
        You're filling every value of `data` with whatever is in `index`. Every time.
        This is not what you want.
        */      
        data[i] = index; 
    }

    System.out.println("Index : " + index);
    //OK
    index = input.indexOf(word, index + word.length());
}

ArrayList 替换您的数据数组和关联的循环。为您找到的每个索引使用ArrayList.add()

【讨论】:

    【解决方案2】:

    如果您尝试生成字符串中某个单词的索引列表,请尝试使用indexOf(String str, int fromIndex) 重载(来自the Java API)。

    编辑:还可以看看这个问题:Stack Overflow: Java Counting # of occurrences of a word in a string

    【讨论】:

      【解决方案3】:

      如果您询问您将使用的结构类型,那么我建议您使用字符串映射(单词名称)到整数列表(这些单词的索引)。

      下面的课程展示了我如何实现一个存储列表的地图。

      
      
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.Iterator;
      import java.util.List;
      import java.util.Set;
      
      /**
       * Class of a map which allows to have a list of items under a single key. 
       * @author Konrad Borowiecki
       *
       * @param <T1> type of the key.
       * @param <T2> type of objects the value list will store.
       */
      public class ListHashMap<T1, T2> extends HashMap<T1, List<T2>>
      {
          private static final long serialVersionUID = -3157711948165169766L;
      
          public ListHashMap()
          {
          }
      
          public void addItem(T1 key, T2 item)
          {
              if(containsKey(key))
              {
                  List<T2> tml = get(key);
                  tml.add(item);
              }
              else
              {
                  List<T2> items = new ArrayList<T2>();
                  items.add(item);
                  put(key, items);
              }
          }
      
          public void removeItem(T1 key, T2 item)
          {
              List<T2> items = get(key);
              items.remove(item);
          }
      
          public void removeItem(T2 item)
          {
              Set<java.util.Map.Entry<T1, List<T2>>> set = entrySet();
              Iterator<java.util.Map.Entry<T1, List<T2>>> it = set.iterator();
      
              while(it.hasNext())
              {
                  java.util.Map.Entry<T1, List<T2>> me = it.next();
                  if(me.getValue().contains(item))
                  {
                      me.getValue().remove(item);
                      if(me.getValue().isEmpty())
                          it.remove();
                      break;
                  }
              }
          }
      }
      
      

      在您的情况下,您将拥有单词到索引列表的映射,因此您可以这样调用该类: ListHashMap wordToIndexesMap = new ListHashMap();

      享受吧,波罗。

      【讨论】:

        猜你喜欢
        • 2013-01-29
        • 2023-03-17
        • 1970-01-01
        • 2022-11-22
        • 2014-03-08
        • 1970-01-01
        • 2016-08-17
        • 2023-04-06
        • 1970-01-01
        相关资源
        最近更新 更多