java - 如何将值存储到具有某些特定索引的数组列表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33716948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to store value into arraylist with some specific index in java
提问by Putra Nurfajar
I have value list such as
我有价值清单,例如
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 0.11, 0.12
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 0.11, 0.12
and I want to store it into ArrayList
with specific index like this
我想将它存储到这样ArrayList
的特定索引中
0.1, 0.2, 0.3, == Index 1
0.4, 0.5, 0.6, == Index 2
0.7, 0.8, 0.9, == Index 3
0.10, 0.11, 0.12, == Index 4
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
Thanks
谢谢
回答by JavaFox
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}}
This is a multi-dimensional array
这是一个多维数组
To set values:
要设置值:
array[0][0] = 0.1;
array[0][1] = 0.2;
array[1][0] = 0.4;
array[1][1] = 0.5;
...ect
...等
Also see How to create a Multidimensional ArrayList in Java?
回答by Kumaresan Perumal
you use array List and HaspMap for you requirement
您使用数组 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));
}
}
回答by Uma Kanth
In an ArrayList<Double>
a single index can have a single value.
在ArrayList<Double>
单个索引中可以有单个值。
If you want to store more than one value try using a ArrayList<ArrayList<Double>>
.
如果您想存储多个值,请尝试使用ArrayList<ArrayList<Double>>
.
If you want to store it in an index use .set(index, value)
如果要将其存储在索引中,请使用 .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);
From your list I see that 3 values make a 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);
}
回答by shakhawat
In arrayList you can't keep multiple value in same index, it just doesn't work that way.
在 arrayList 中,您不能在同一个索引中保留多个值,它只是不能那样工作。
Based on your requirement, i think, you can design the data structure in two way -
根据您的要求,我认为您可以通过两种方式设计数据结构 -
1. Map
1. 地图
Map<Integer, List<Double>>
a map keeping index as key, and list of the items as value. Thus keep (1, [0.1, 0.2, 0.3]) means 1 index as key and corresponding list as value
以索引为键,以项目列表为值的映射。因此 keep (1, [0.1, 0.2, 0.3]) 表示 1 个索引作为键,对应的列表作为值
2. List of User Defined Object
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));