• 写一个查找算法找出数组中相同的元素


     1     import java.util.ArrayList;  
     2       
     3     public class Test {  
     4       
     5         // 原始数据data。假设data数组中的数据元素已经按照某种顺序排好。  
     6         // 但是,该数组中的数据元素重复出现。  
     7         // 我们的目的是查找、解析data数组中重复出现的某元素。  
     8         // 比如,在这个data数组中,元素'C'在数组位置2,3重复出现两次。  
     9         // 注意!有些元素没有重复出现,比如元素'B'。  
    10         private String[] data = { "A", "A", "B", "C", "C", "D", "D", "D" };  
    11       
    12         // 存储分类好的数据元素。  
    13         private ArrayList<Group> groups = new ArrayList<Group>();  
    14       
    15         // 核心的算法实现。  
    16         public void find() {  
    17       
    18             // 游标index  
    19             int index = 0, j = 0;  
    20       
    21             while (index < data.length) {  
    22                 Group group = new Group();  
    23                 group.title = data[index];  
    24                   
    25                 String t = group.title;  
    26       
    27                 ArrayList<String> children = new ArrayList<String>();  
    28       
    29                 for (j = index; j < data.length; j++) {  
    30       
    31                     String child = data[j];  
    32                     if (t.equals(child)) {  
    33                         // 同时记录该重复出现的元素在原数组中的下标j,便于查验、评估结果。  
    34                         children.add(child + "@" + j);  
    35                     } else {  
    36                         break;  
    37                     }  
    38                 }  
    39       
    40                 // 往后推进游标index  
    41                 index = j;  
    42       
    43                 group.children = children;  
    44                 groups.add(group);  
    45             }  
    46         }  
    47       
    48         // 输出结果。  
    49         private void print() {  
    50             for (int i = 0; i < groups.size(); i++) {  
    51                 Group g = groups.get(i);  
    52                 System.out.println(g);  
    53             }  
    54         }  
    55       
    56         // 自己构造一个类,作为一组数据的容器。  
    57         // 该类用一个title表明这一group数据是归属于那个重复元素的组。  
    58         // 该title下重复的元素装入到ArrayList<String> children中,供遍历查询。  
    59         private class Group {  
    60             public String title;  
    61             public ArrayList<String> children;  
    62       
    63             // 结果。  
    64             @Override  
    65             public String toString() {  
    66                 String str = "组" + title + ": ";  
    67                 for (int i = 0; i < children.size(); i++) {  
    68                     str += children.get(i) + " ";  
    69                 }  
    70       
    71                 return str;  
    72             }  
    73         }  
    74       
    75         public static void main(String args[]) {  
    76             Test t = new Test();  
    77             t.find();  
    78             t.print();  
    79         }  
    80     }  
  • 相关阅读:
    区块链系列教程
    第三章 通过java SDK 实现个性化智能合约的部署与测试
    第一章 区块链系列 联盟链FISCO BCOS 底层搭建
    ABP 框架 数据库底层迁移 Mysql 集群
    ABP 框架代码批量生成器
    基于百度理解与交互技术实现机器问答
    微软人工智能和对话平台--知识商城体验
    基于百度AI实现 车牌识别
    最近整理AI相关感想
    百度OCR文字识别-身份证识别
  • 原文地址:https://www.cnblogs.com/yoyohong/p/5762667.html
Copyright © 2020-2023  润新知