计算arraylist

时间:2016-04-07 18:16:08

标签: java arraylist

这是我的代码

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import static com.sun.org.apache.xalan.internal.lib.ExsltStrings.split;
import static java.rmi.Naming.list;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import static java.util.Collections.list;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author dhanushka
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String s ="HBVR7RATFCHEGJ312ZKS7D14X6977QONSGJYUK7SU5VF8RDOOX29AFJDJ13BRCWZY3W99EGS118CSW5G3VTJYPNN7GP3Q6BSI6RE5DOJRKHJGGVX6XH6SMBIQJC2YNB50Q4VE0L0GJ2OHDG0V273ZF965IXVBWVDIMS0M9SFMQZJTXBNCLH6PIGVQ706CZYUYZF5VPNVJ1QIPZMIHKSZOZYAG7JOIWCW2EV4RG1X7VWY17F1USY1G9F8BFZXF9NXY4QLES0AS513C9JI2S4Z7LP28W3MRER8I996R4WC2XILHM6B5Q5WEXZNOXMSYNTON3UDAM08617JCEVVZ2QAJ43UKVS4RENOE47O22AT060NZPLKB0267TET993P36Q2NTFHGH3727JMDQ44JH20LRRUZOO29UZ4Q89GJDE465EAJ2PTE8FWD0Q6CVS12B7HHP6FQYUGD73QMQK695XAPD00SDEYVMB576236JCQO9SG6ATVKV1Y6C0MDGXNU6U068SPYQAZ2GWQVU3STSNAVEQAFYE0WZ6RO2J8RD73UCEIKLSFMF3BSUF1Y6C0MDDCKEB3K5AQVUPT9I03G8OR6594WSQ927QK9C7GKZ5QEMP7FW1J2R3O5VGPEHG1PBXYDET7KTSQD7NTPBZA0GHKECON5SBDTDBCZ14AC48I9ML8HW9CSGBI4MTBZGB2SP795L4YHY76H8L10QFJS8UNC2HMH2K62GZTPMD62L53IFA5A2LJA2JU4U7DDRQPNBDBJ2LONIRGD1E61LMPRB5A7FY5O93K5PPGB9WIQ661LCQDPAL1FA8MGDEDEQ9NPGLXL852ZD9UEDY4YUQOYCF4RD985SEY6C0MD22QFSBBBNHCOJPTAVYNAQGH867G99XV1TD629JFAD3D67UVDU9M49EKWURJV5BB0VL4FT0ALSM6WDKBMLCOAK02P3JHRAUFB2MKVZQU48AMWHXSJQ7K2XBP4TPMJTTL8H441APW";
       // String s = "ABCDE";
       String[] strValues = s.split("");

       ArrayList<String> aListNumbers = new ArrayList<String>(Arrays.asList(strValues));
       //System.out.println("Java String converted to ArrayList: " + aListNumbers);
       aListNumbers.remove(0);
       //System.out.println("Java String converted to ArrayList: " + aListNumbers);
       if(aListNumbers.size()%2==1) {
         aListNumbers.add("");
       }

       List<List<String>> list = new ArrayList<List<String>>();

       for (int x = 0;x<aListNumbers.size()-1;x++) {
           List<String> l = new ArrayList<String>();
           l.add(aListNumbers.get(x));
           l.add(aListNumbers.get(x+1));
           list.add(l);
           x++;
       }

       System.out.println("List "+list);
       // System.out.println("List "+list.toString());
    }   
}

OUTPUT 运行:

列出[[H,B],[V,R],[7,R],[A,T],[F,C],[H,E],[G,J],[3, 1],[2,Z],[K,S],[7,D],[1,4],[X,6],[9,7],[7,Q],[O,N] ,[S,G],[J,Y],[U,K],[7,S],[U,5],[V,F],[8,R],[D,O],[ O,X],[2,9],[A,F],[J,D],[J,1],[3,B],[R,C],[W,Z],[Y, 3],[W,9],[9,E],[G,S],[1,1],[8,C],[S,W],[5,G],[3,V] ,[T,J],[Y,P],[N,N],[7,G],[P,3],[Q,6],[B,S],[I,6],[ R,E],[5,D],[O,J],[R,K],[H,J],[G,G],[V,X],[6,X],[H, 6],[S,M],[B,I],[Q,J],[C,2],[Y,N],[B,5],[0,Q],[4,V] ,[E,0],[L,0],[G,J],[2,O],[H,D],[G,0],[V,2],[7,3],[ Z,F],[9,6],[5,I],[X,V],[B,W],[V,D],[I,M],[S,0],[M, 9],[S,F],[M,Q],[Z,J],[T,X],[B,N],[C,L],[H,6],[P,I] ,[G,V],[Q,7],[0,6],[C,Z],[Y,U],[Y,Z],[F,5],[V,P],[ N,V],[J,1],[Q,I],[P,Z],[M,I],[H,K],[S,Z],[O,Z],[Y, A],[G,7],[J,O],[I,W],[C,W],[2,E],[V,4],[R,G],[1,X] ,[7,V],[W,Y],[1,7],[F,1],[U,S],[Y,1],[G,9],[F,8], [B,F],[Z,X],[F,9],[N,X],[Y,4],[Q,L],[E,S],[0,A],[S ,5],[1,3],[C,9],[J,I],[2,S],[4,Z],[7,L],[P,2],[8,W ],[3,M],[R,E],[R,8],[I,9],[9,6],[R,4],[W,C],[2,X], [I,L],[H,M],[6,B],[5,Q],[5,W],[E,X],[Z,N],[O,X],[M ,S],[Y,N],[T,O],[N,3],[U,D],[A,M],[0,8],[6,1],[7,J ],[C,E],[V,V],[Z,2],[Q,A],[J,4],[3,U],[K,V],[S,4], [R,E],[N,O],[E,4],[7,O],[2,2],[A,T],[0,6],[0,N],[Z ,P],[L,K],[B,0],[2,6],[7,T],[E,T],[9,9],[3,P],[3,6 ],[Q,2],[N,T],[F,H],[G,H],[3,7],[2,7],[J,M],[D,Q], [4,4],[J,H],[2,0],[L,R],[R,U],[Z,O],[O,2],[9,U],[Z ,4],[Q,8],[9,G],[J,D],[E,4],[6,5],[E,A],[J,2],[P,T] ],[E,8],[F,W],[D,0],[Q,6],[C,V],[S,1],[2,B],[7,H], [H,P],[6,F],[Q,Y],[U,G],[D,7],[3,Q],[M,Q],[K,6],[9 ,5],[X,A],[P,D],[0,0],[S,D],[E,Y],[V,M],[B,5],[7,6 ],[2,3],[6,J],[C,Q],[O,9],[S,G],[6,A],[T,V],[K,V], [1,Y],[6,C],[0,M],[D,G],[X,N],[U,6],[U,0],[6,8],[S ,P],[Y,Q],[A,Z],[2,G],[W,Q],[V,U],[3,S],[T,S],[N,A ],[V,E],[Q,A],[F,Y],[E,0],[W,Z],[6,R],[O,2],[J,8], [R,D],[7,3],[U,C],[E,I],[K,L],[S,F],[M,F],[3,B],[S ,U],[F,1],[Y,6],[C,0],[M,D],[D,C],[K,E],[B,3],[K,5 ],[A,Q],[V,U],[P,T],[9,I],[0,3],[G,8],[O,R],[6,5], [9,4],[W,S],[Q,9],[2,7],[Q,K],[9,C],[7,G],[K,Z],[5] ,Q],[E,M],[P,7],[F,W],[1,J],[2,R],[3,O],[5,V],[G,P ],[E,H],[G,1],[P,B],[X,Y],[D,E],[T,7],[K,T],[S,Q], [D,7],[N,T],[P,B],[Z,A],[0,G],[H,K],[E,C],[O,N],[5 ,S],[B,D],[T,D],[B,C],[Z,1],[4,A],[C,4],[8,I],[9,M ],[L,8],[H,W],[9,C],[S,G],[B,I],[4,M],[T,B],[Z,G], [B,2],[S,P],[7,9],[5,L],[4,Y],[H,Y],[7,6],[H,8],[L ,1,[0,Q],[F,J],[S,8],[U,N],[C,2],[H,M],[H,2],[K,6 ],[2,G],[Z,T],[P,M],[D,6],[2,L],[5,3],[I,F],[A,5], [A,2],[L,J],[A,2],[J,U],[4,U],[7,D],[D,R],[Q,P],[N ,B],[D,B],[J,2],[L,O],[N,I],[R,G],[D,1],[E,6],[1,L ],[M,P],[R,B],[5,A],[7,F],[Y,5],[O,9],[3,K],[5,P], [P,G],[B,9],[W,I],[Q,6],[6,1],[L,C],[Q,D],[P,A],[L ,1,[F,A],[8,M],[G,D],[E,D],[E,Q],[9,N],[P,G],[L,X ],[L,8],[5,2],[Z,D],[9,U],[E,D],[Y,4],[Y,U],[Q,O], [Y,C],[F,4],[R,D],[9,8],[5,S],[E,Y],[6,C],[0,M],[D ,2],[2,Q],[F,S],[B,B],[B,N],[H,C],[O,J],[P,T],[A,V] ],[Y,N],[A,Q],[G,H],[8,6],[7,G],[9,9],[X,V],[1,T], [D,6],[2,9],[J,F],[A,D],[3,D],[6,7],[U,V],[D,U],[9 ,M],[4,9],[E,K],[W,U],[R,J],[V,5],[B,B],[0,V],[L,4 ],[F,T],[0,A],[L,S],[M,6],[W,D],[K,B],[M,L],[C,O], [A,K],[0,2],[P,3],[J,H],[R,A],[U,F],[B,2],[M,K],[V ,Z],[Q,U],[4,8],[A,M],[W,H],[X,S],[J,Q],[7,K],[2,X ],[B,P],[4,T],[P,M],[J,T],[T,L],[8,H],[4,4],[1,A], [P,W]] 建立成功(总时间:1秒)

我想得到最后一个列表中相等元素的数量

2 个答案:

答案 0 :(得分:1)

您的代码将原始字符串拆分为2个连续字符组。

如果你想检查这两个连续的字符是否相等,你可以这样做:

char[] chars = s.toCharArray();
int count = 0;
for(int i = 0 ; i < chars.length - 1 ; i += 2) {
    count += chars[i] == chars[i+1] ? 1 : 0;
}

答案 1 :(得分:0)

此代码将计算列表中相等列表的数量。

 Set<String> set = new HashSet<>();
    int count = 0;
    for (ArrayList<String> list : lists) {
        String s = list.get(0) + list.get(1);
        if (set.contains(s)) {
            count++;
        } else {
            set.add(s);
        }
    }
    System.out.println("equal elemets: " + count);