【问题标题】:how to store value into arraylist with some specific index in java [closed]如何在java中使用某些特定索引将值存储到arraylist中[关闭]
【发布时间】:2015-11-15 06:03:47
【问题描述】:

我有价值列表,例如

0.1、0.2、0.3、0.4、0.5、0.6、0.7、0.8、0.9、0.10、0.11、0.12

我想用这样的特定索引将它存储到ArrayList

0.1, 0.2, 0.3, == 索引 1

0.4, 0.5, 0.6, == 索引 2

0.7, 0.8, 0.9, == 索引 3

0.10, 0.11, 0.12, == 索引 4

谢谢

【问题讨论】:

  • 要求很不明确,但是List.set(index, value) 做你想做的事吗?
  • 我还没试过,我不明白这个。因为我想用循环存储该列表
  • 我可以用循环 for 做List.set(index, value) 吗?因为索引是动态的

标签: java list arraylist collections


【解决方案1】:

ArrayList<Double> 中,单个索引可以有单个值。

如果您想存储多个值,请尝试使用ArrayList<ArrayList<Double>>

如果您想将其存储在索引中,请使用 .set(index, value)

ArrayList<Double> list = new ArrayList<>();

list.add(0.1);
list.add(0.2);
list.add(0.3);

ArrayList<ArrayList<Double>> mainList = new ArrayList();

mainList.set(0, list);

从您的列表中,我看到 3 个值构成了一个列表。

double []values = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 0.11, 0.12};

ArrayList<ArrayList<Double>> mainList = new ArrayList();
int k = 0;
for(int i = 0; i < values.length; i += 3)
{
  ArrayList<Double> list = new ArrayList();
  list.add(values[i]);
  list.add(values[i + 1]);
  list.add(values[i + 2]);

  mainList.set(k ++, list);
}

【讨论】:

  • 感谢您的回答。我会试试看。 .谢谢 。 .
【解决方案2】:
ArrayList[][] array = {{0.1,0.2,0.3},{0.4,0.5,0.6},{0.7,0.8,0.9},{0.10,0.11,0.12}}

这是一个多维数组

设置值:

array[0][0] = 0.1;
array[0][1] = 0.2;
array[1][0] = 0.4;
array[1][1] = 0.5;

...等

另见How to create a Multidimensional ArrayList in Java?

【讨论】:

    【解决方案3】:

    在 arrayList 中,你不能在同一个索引中保留多个值,它只是不能那样工作。

    我认为,根据您的要求,您可以通过两种方式设计数据结构 -


    1.地图

    Map<Integer, List<Double>>
    

    将索引作为键,将项目列表作为值的映射。 因此 keep (1, [0.1, 0.2, 0.3]) 表示 1 个索引作为键,对应的列表作为值

    2。用户定义对象列表


    class IndexValue {
      int index;
      double value;
      IndexValue(int index, double value) {
         this.index = index;
         this.value = value;
      }
    }
    
    List<IndexValue> indexValueList = new ArrayList<>();
    indexValueList.add(new IndexValue(1, .1));
    indexValueList.add(new IndexValue(1, .2));
    indexValueList.add(new IndexValue(1, .3));
    

    【讨论】:

    • 感谢您的回答。我会试试看。 .谢谢
    • 取决于 OP 试图做什么,它也可能是一个 Map,但是:Map&lt;Double, Integer&gt;,其值:((0.1, 1), (0.2, 1), (0.3, 1), (0.4, 2), (0.5, 2), ...)。但不知道他为什么想要它,很难说......
    【解决方案4】:

    您可以根据需要使用数组 List 和 HaspMap

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    public class Test {
    
        public static void main(String[] args) {
            ArrayList<Double> list  = new ArrayList<Double>();  
            Map<String, ArrayList<Double>> map = new HashMap<String, ArrayList<Double>>();
    
            list.add(0.1);
            list.add(0.2);
    
            map.put("key", list);
    
            System.out.println(map.get("key").get(0));
        }   
    }
    

    【讨论】:

    • @ChiefTwoPencils :现在我改变了它。看看吧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多