我有一个数据需要以Double[]
的形式传递,
我的数据在double[]
中,我需要以Double[]
的身份通过
将其更改为List<Double>
这是我的代码
ListFragment
@Override
public void OnEditItem(int position) {
Model selectedItem = mModel.get(position);
String selectedKey = selectedItem.getKey();
String selectedImage = selectedItem.getImagesUri();
String selecteDescriptions = selectedItem.getImageDescription();
String selectedImagesName = selectedItem.getImageNames();
List<Double> selectedataSet = selectedItem.getDataSet();
double[] zeroSet = new double[selectedataSet.size()];
for(int i=0; i<zeroSet.length; i++){
zeroSet[i] = selectedataSet.get(i);
}
Log.d(TAG, "thisList: " + datacel);
EditFragment editFragment = new EditFragment();
FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager()
.beginTransaction();
final Bundle bundle = new Bundle();
bundle.putString("Label", selectedKey);
bundle.putString("Image", selectedImage);
bundle.putString("Descriptions", selecteDescriptions);
bundle.putString("ImagesName", selectedImagesName);
这是接收double[]
EditFragment
private void updateView(View view) {
if(getArguments()!=null){
String labelId = getArguments().getString("Label");
String imagesBundle = getArguments().getString("Image");
String descriptionsBundle =getArguments().getString("Descriptions");
String imagesNameBundle =getArguments().getString("ImagesName");
double[] dataSet = getArguments().getDoubleArray("DataSet");
//this i where i need to change into Double[];
Log.d(TAG, "thisLIst: " + dataSet);
etName.setText(String.valueOf(imagesNameBundle));
etDescribe.setText(descriptionsBundle);
Picasso.get().load(imagesBundle).into(ImageInput);
simpleActivity.setDataToListView(value, listViewDataSet,context); // this value need Double[];
已编辑
我认为我的错误从这里开始
double[] zeroSet = new
double[selectedataSet.size()];
for(int i=0; i<zeroSet.length; i++){
zeroSet[i] = selectedataSet.get(i);
}
因为我得到的数据就是这样
我该如何解决?
答案 0 :(得分:1)
尝试这样的事情:
import java.util.Arrays;
class BoxArrayExample {
public static void main(String[] args) {
double[] arr = new double[] {1.0, 2.0, 3.0};
System.out.println(String.format("Unboxed array: %s", Arrays.toString(arr)));
Double[] boxedArr = Arrays.stream(arr)
.boxed()
.toArray(Double[]::new);
System.out.println(String.format("Boxed array: %s", Arrays.toString(boxedArr)));
}
}
输出:
Unboxed array: [1.0, 2.0, 3.0]
Boxed array: [1.0, 2.0, 3.0]
答案 1 :(得分:1)
一点也不复杂。您只需要自己复制数组元素,例如
double[] dataSet = getArguments().getDoubleArray("DataSet");
Double[] boxedDataSet = new Double[dataSet.length];
for ( int i = 0; i < dataSet.length; i++ ) {
boxedDataSet[i] = dataSet[i]; // Invokes auto-boxing
// Equivalent, more explicit alternative:
// boxedDataSet[i] = Double.valueOf(dataSet[i]);
}