开发者社区> 问答> 正文

java如何找出数组中相同的数并定位其位置

java如何找出数组中相同的数并定位其位置
目前有两个数组,位置一一对应,我想通过矩阵一找到出力相同的出力位置在通过位置定位矩阵二中的数并取其最小值比如14对应的各个流量
出力矩阵
0 7 8 9 10 11 12 13 {14}
5 12 13 {14} 15 16 17 18 19
6 13 {14} 15 16 17 18 19 20
7 {14} 15 16 17 18 19 20 21
8 15 16 17 18 19 20 21 22
9 16 17 18 19 20 21 22 23
10 17 18 19 20 21 22 23 24
11 18 19 20 21 22 23 24 25
12 19 20 21 22 23 24 25 26
流量矩阵
0.0 346.7 395.3 444.7 493.9 545.0 598.1 652.0 {709.1}
251.0 597.7 646.3 {695.7} 744.9 796.0 849.1 903.0 960.1
297.9 644.6 {693.2} 742.6 791.8 842.9 896.0 949.9 1007.0
344.5 {691.2} 739.8 789.2 838.4 889.5 942.6 996.5 1053.6
393.1 739.8 788.4 837.8 887.0 938.1 991.2 1045.1 1102.2
445.8 792.5 841.1 890.5 939.7 990.8 1043.9 1097.8 1154.9
500.5 847.2 895.8 945.2 994.4 1045.5 1098.6 1152.5 1209.6
555.0 901.7 950.3 999.7 1048.9 1100.0 1153.1 1207.0 1264.1
610.4 957.1 1005.7 1055.1 1104.3 1155.4 1208.5 1262.4 1319.5
我想到的解法是这样做但是我不知道接下来怎么做了,不知道各位有没有办法

展开
收起
zljhhka 2018-08-18 15:50:47 3173 0
1 条回答
写回答
取消 提交回答
  • 自由散漫的开发者,哇哈哈

    定义一个对象存一下,应该满足题意吧,参考代码如下:

    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @ClassName: Entry
     * @Description: TODO
     * @author jiang
     * @date 2018年8月22日 下午1:47:46
     * 
     */
    public class Entry {
    
        /**
         * 数值
         */
        private Integer value;
    
        /**
         * 位置
         */
        private List<Integer[]> index;
    
        public Integer getValue() {
            return value;
        }
    
        public void setValue(Integer value) {
            this.value = value;
        }
    
        public List<Integer[]> getIndex() {
            return index;
        }
    
        public void setIndex(List<Integer[]> index) {
            this.index = index;
        }
    
        public static void main(String[] args) {
            // 出力矩阵数字列表
            List<Entry> entryList = new ArrayList<Entry>();
    
            // 循环出力矩阵,假如当前是x
            Integer x = 14;
            // 单个数字
            Entry entry = null;
            /****** start 在这里包一个矩阵循环,给x赋值,同时也是index的值 ***************************/
            // 数字列表取值或新建
            for (Entry en : entryList) {
                if (!en.getValue().equals(x)) {
                    continue;
                }
                entry = en;
                break;
            }
            if (entry == null) {
                entry = new Entry();
                entry.setValue(x);
                entryList.add(entry);
            }
            // 放入数字位置
            Integer[] index = new Integer[2];
            index[0] = 2;
            index[1] = 3;
    
            List<Integer[]> indexList = entry.getIndex();
            if (indexList == null) {
                indexList = new ArrayList<Integer[]>();
                entry.setIndex(indexList);
            }
            indexList.add(index);
            /***** end ****************************/
    
            // 最后得到一个entry的列表,entry有数字和位置列表,value是数字,list的长度就是出现的数量,之后就随便操作了
        }
    
    }
    2019-07-17 23:01:25
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载