另一个工具类,获取所有下标值

接收两个字符串参数,然后返回一个数组,内容是第一个字符串在第二个字符串中所有的下标值。

感觉将来会发现其实这个已经有内置类替我解决了……

数组列表和数组的转换有点蛋疼,查到的说法都不能用,还有人在用我这没有的方法……我搜的明明就是JAVA……

结果还是用笨方法了,看StackOverFlow上很多人也是这么办的。

吃完饭再稍微修改修改。

 1 package selfUtil;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class GetAllIndex {
 6     
 7     /** Get all indexes of a char or a piece of String in another String. */
 8     public static int[] getAllIndex(String part, String all) {
 9         // Initialize things.
10         int index = 0;
11         ArrayList<Integer> temp = new ArrayList<Integer>();
12         
13         // Get all indexes and put them into an ArrayList.
14         while (true) {
15             index = all.indexOf(part,index);
16             if (index == -1)
17                 break;
18             
19             temp.add(index);
20             index++;
21         }
22         
23         // Convert the ArrayList into an array.
24         int[] result = new int[temp.size()];
25         for (int i = 0; i < temp.size(); i++) {
26             result[i] = temp.get(i).intValue();
27         }
28         
29         return result;
30     }
31 }
单数复数单数复数
posted @ 2013-11-25 13:15  Chihane  阅读(153)  评论(0编辑  收藏  举报