cowthan 发表于 2013-4-28 16:09
先表达一下对这个问题的不理解,再表达一下我的理解: - public class ArrayTest {
- public static void main(String[] args){
- String[] str = new String[] { "a,b", "b,c", "c,d", "b,e", "e,f","h,g" };
- //str的copy,不做改变
- String[] strCopy = str;
- //存放最终的list
- List<String> strList = new ArrayList<String> ();
- /**
- * a,b可能被其他的公用拼接字符串,那么从前往后拼接
- */
-
- for(int i=0;i<str.length;i++){
- String[] strArr = str[i].split(",");
- //str第i个字符是否和其他字符拼接,若拼接则不往strList中添加
- boolean flag = false;
- for(int ii=0;ii<strCopy.length;ii++){
- String[] strArrCopy = strCopy[ii].split(",");
-
- //如果str[i]的开始字符和strcopy[ii]的结束字符相同,拼接str的第ii个元素(a,b,b,c 替换b,b为b)
- if(strArr[strArr.length-1].equals(strArrCopy[0])){
- str[ii] = (str[i]+","+str[ii]).replace(strArrCopy[0]+","+strArrCopy[0], strArrCopy[0]);
- flag = true;
- }
- }
- if(!flag){
- strList.add(str[i]);
- }
- }
-
- for(String s:str){
- System.out.println(s);
- }
- System.out.println("****************");
- for(String s:strList){
- System.out.println(s);
- }
- }
- }
复制代码 不知看完后能否理解呢! |