Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将(浮点)ArrayList中的值保存到捆绑包中_Arrays_Arraylist_Floating Point_Integer_Bundle - Fatal编程技术网

Arrays 将(浮点)ArrayList中的值保存到捆绑包中

Arrays 将(浮点)ArrayList中的值保存到捆绑包中,arrays,arraylist,floating-point,integer,bundle,Arrays,Arraylist,Floating Point,Integer,Bundle,我正在使用Surfaceview编写一个游戏,我有一个关于将数据保存到捆绑包中的问题 最初,我有一个arraylist,它以精灵整数的形式存储Y坐标,精灵只能上下移动。声明为: static ArrayList<Integer> ycoordinates = new ArrayList<Integer>(); 并使用以下命令恢复它们: ycoordinates.addAll(savedState.getIntegerArrayList("ycoordinates"));

我正在使用Surfaceview编写一个游戏,我有一个关于将数据保存到捆绑包中的问题

最初,我有一个arraylist,它以精灵整数的形式存储Y坐标,精灵只能上下移动。声明为:

static ArrayList<Integer> ycoordinates = new ArrayList<Integer>();
并使用以下命令恢复它们:

ycoordinates.addAll(savedState.getIntegerArrayList("ycoordinates"));
这一切都非常有效。然而,我不得不改变整个坐标系统,因此它基于Delta时间,以允许我的精灵在不同屏幕上以统一的速度移动。这是,再次,工作完美

但是,由于这个更改,我现在必须将这些值存储为浮点数而不是整数

因此,我宣布:

static ArrayList<Float> ycoordinates = new ArrayList<Float>();
这就是背景,现在我的问题是,如何存储和恢复浮点数组列表中的值?似乎没有putFloatArrayList或getFloatArrayList

我使用了Arraylist而不是数组,因为精灵的数量需要是动态的

任何帮助都将不胜感激


非常感谢

我写了两个简单的方法来在List和float[]之间进行转换。您可以在float[]上使用Bundle putFloatArray和getFloatArray

import java.util.ArrayList;
import java.util.List;


public class Test {
  public static void main(String[] args){
    List<Float> in = new ArrayList<Float>();
    in.add(3.0f);
    in.add(1f);
    in.add((float)Math.PI);
    List<Float>out = toList(toArray(in));
    System.out.println(out);
  }

  public static float[] toArray(List<Float> in){
    float[] result = new float[in.size()];
    for(int i=0; i<result.length; i++){
      result[i] = in.get(i);
    }
    return result;
  }

  public static List<Float> toList(float[] in){
    List<Float> result = new ArrayList<Float>(in.length);
    for(float f : in){
      result.add(f);
    }
    return result;
  }

}

精灵在捆绑包中的数量可能不会改变,因此您可以将其存储为数组,并在恢复时转换回列表。好主意Patricia,谢谢,知道如何将Arraylist转换为数组吗?这是一件简单的事情吗?再次感谢。我建议阅读列表toArray方法。再次感谢,我会检查此方法。再考虑一下,你需要一个float[],而不是float[]toArray会给你的。我已经编写了两个实用方法来进行转换,并在我的答案中发布了它们。
import java.util.ArrayList;
import java.util.List;


public class Test {
  public static void main(String[] args){
    List<Float> in = new ArrayList<Float>();
    in.add(3.0f);
    in.add(1f);
    in.add((float)Math.PI);
    List<Float>out = toList(toArray(in));
    System.out.println(out);
  }

  public static float[] toArray(List<Float> in){
    float[] result = new float[in.size()];
    for(int i=0; i<result.length; i++){
      result[i] = in.get(i);
    }
    return result;
  }

  public static List<Float> toList(float[] in){
    List<Float> result = new ArrayList<Float>(in.length);
    for(float f : in){
      result.add(f);
    }
    return result;
  }

}