如何使用Java创建数组列表

时间:2019-04-30 04:47:45

标签: java arrays arraylist

我有一个值为{a,b,a,c,d,b,a}的arrayList 我想对列表中的每个元素进行比较,然后将pair的{​​{1}}插入到common indexes或使用Java的东西中

示例输出:[[0,2,6],[1,4]] 说明:List of array位于索引a,而0,2,6位于索引b 到目前为止,我有这个:

1,4

更新: 想法是将[[0,4],[1,3],[3,1]]作为 HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>(); List<String> name = new ArrayList<String>(); letter.add("a"); letter.add("b"); letter.add("c"); letter.add("b"); letter.add("a"); for (int i = 0; i < letter.size(); i++) { for (int j = 1; j < letter.size(); j++) { if (letters.get(i).equals(letters.get(j)) && i != j) { hashMap.put(i, j); } } } System.out.println(hashMap); //o/p: {0=4, 1=3, 3=1} List<int[]> myList = new ArrayList<int[]>(); Iterator entries = hashMap.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); Integer key = (Integer)entry.getKey(); Integer value = (Integer)entry.getValue(); myList.add(new int[] {key,hashMap.get(key)}); } System.out.println(myList.toString()); //O/P: [[I@380fb434, [I@668bc3d5, [I@3cda1055] 中的元素,但我无法做到这一点。任何帮助深表感谢!谢谢!

基于上述索引数组,我想比较这些索引处不同myList ListB中的元素-意思是比较索引C中的元素在0,2,6中检查所有三个元素是否相等。索引List B and C上的元素相同

3 个答案:

答案 0 :(得分:2)

我认为这会对您有所帮助:

    ArrayList<int[]> arrayList =new ArrayList<>();
    int[] arrayItem={0,2,6};
    int[] arrayItem2={1,4};
    arrayList.add(arrayItem);
    arrayList.add(arrayItem2);

答案 1 :(得分:1)

作业已完成,请检查以下内容:

package com.company;

import java.util.*;

public class Main {

    public static void main(String[] args) {
    // write your code here
        HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
        List<String> letter = new ArrayList<String>();
        letter.add("a");
        letter.add("b");
        letter.add("c");
        letter.add("b");
        letter.add("a");

        for (int i = 0; i < letter.size(); i++) {
            for (int j = 1; j < letter.size(); j++) {
                if (letter.get(i).equals(letter.get(j)) && i != j) {
                    hashMap.put(i, j);
                }
            }
        }
        System.out.println(hashMap); //o/p: {0=4, 1=3, 3=1}
        List<int[]> myList = new ArrayList<int[]>();
        Iterator entries = hashMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            Integer key = (Integer)entry.getKey();
            Integer value = (Integer)entry.getValue();
            int[] intValues = new int[2];
            intValues[0] = key;
            intValues[1] = value;
            myList.add(intValues);
        }

        String toPrint = new String();
        toPrint = toPrint.concat("[");
        for(int k = 0; k < myList.size(); k++) {
            toPrint = toPrint.concat("[");
            for(int l = 0; l < myList.get(k).length; l++) {
                toPrint = toPrint.concat(String.valueOf(myList.get(k)[l]));
                if(l != (myList.get(k).length-1)){
                    toPrint = toPrint.concat(",");
                }
            }
            toPrint = toPrint.concat("]");
        }
        toPrint = toPrint.concat("]");

        System.out.println(toPrint);
        //[[0,4][1,3][3,1]]
    }
}

如果您需要做的只是在屏幕上打印值,那么此代码将为您工作。

答案 2 :(得分:1)

您不能使用Map来检查String的所有索引。您可以尝试使用ArrayList,如下所示,

import java.util.*;

public class ListCharIndexes {
    public static void main(String[] args) {
        List<String> letter = Arrays.asList("a","b","a","c","d","b","a");
       //letter= Arrays.asList("a","b","c","b","a");
        List<List<Integer>> result=new ArrayList<>();
        Set<String> result1=new HashSet<>();
        for (int i = 0; i < letter.size(); i++) {
            if(result1.add(letter.get(i))){ //skip String if it is already processed
                List<Integer> indexes=indexOfAll(letter.get(i), letter);
                if(indexes.size()>1)     //add only pairs
                    result.add(indexes);
            }
        }
        System.out.println(result);
    }
    static List<Integer> indexOfAll(String obj, List<String> list) {
        final List<Integer> indexList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++)
            if (obj.equals(list.get(i))) 
                indexList.add(i);
        return indexList;
    }
}

O / P:

  

[[0,2,6],[1,5]]