我有一个名为stockList
的整数的arraylist。
我希望从arrayList中创建一个原始类型的int [] stockListFinal
。
的代码: 的
public class Warehouses {
ArrayList<Warehouse> warehouses = new ArrayList<Warehouse>();
ArrayList<Integer> stockList = new ArrayList<Integer>();
ArrayList<String> locationsList = new ArrayList<String>();
int[] stockListFINAL;
int[] locationsListFINAL;
public Warehouses() {
addWarehouses();
stockList();
locationList();
}
public void addWarehouses() {
//DATABASE WOULD BE SEARCHED FOR EACH PRESENT RECORD AND ADDED HERE
//FOR SIMPLICITY I HAVE DONE THIS MANUALLY
warehouses.add(new Warehouse("W1", 20, "RM13 8BB"));
warehouses.add(new Warehouse("W2", 28, "RM13 8BB"));
warehouses.add(new Warehouse("W3", 17, "RM13 8BB"));
}
public void stockList() {
for(Warehouse warehouse : warehouses) {
Integer stock = warehouse.getStock();
}
stockListFINAL = convertIntegers(stockList);
}
public static int[] convertIntegers(List<Integer> integers)
{
int[] ret = new int[integers.size()];
for (int i=0; i < ret.length; i++)
{
ret[i] = integers.get(i);
}
return ret;
}
public void locationList() {
for(Warehouse warehouse : warehouses) {
String location = warehouse.getLocation();
}
}
}
class Warehouse
{
private String warehouseID;
private int warehouseStock;
private String location;
/**
* Constructor for objects of class Warehouse
*/
public Warehouse(String warehouseID, int warehouseStock, String location)
{
// initialise instance variables
this.warehouseID = warehouseID;
this.warehouseStock = warehouseStock;
this.location = location;
}
public int getStock(){
return warehouseStock;
}
public String getLocation() {
return location;
}
}
stockListFinal
但是返回空。
这里发生了什么?
答案 0 :(得分:0)
您没有致电addStock()
,因此列表为空。
addStock(); // this was missing, and it
// should probably take the
// List as an argument.
int[] stockListFINAL = convertIntegers(stockList);
然后我用这段代码验证了
// No change.
public static int[] convertIntegers(
List<Integer> integers) {
int[] ret = new int[integers.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = integers.get(i).intValue();
}
return ret;
}
// Made static, added List as argument.
public static void addStock(List<Integer> stockList) {
stockList.add(20);
stockList.add(30);
}
public static void main(String s[]) {
ArrayList<Integer> stockList = new ArrayList<Integer>();
addStock(stockList);
int[] stockListFINAL = convertIntegers(stockList);
System.out.println(java.util.Arrays.toString(stockListFINAL));
}
它输出 -
[20, 30]
您从未将库存添加到列表中。
答案 1 :(得分:0)
如果您在下面的课程中运行main
,则会看到int[]
已填充。这不是我建议实施的方式,但我尽量保持尽可能接近你的例子:
import java.util.ArrayList;
import java.util.List;
public class SE
{
final static ArrayList<Integer> stockList = new ArrayList<Integer>();
public static int[] convertIntegers(final List<Integer> integers)
{
final int[] ret = new int[integers.size()];
for (int i = 0; i < ret.length; i++)
{
ret[i] = integers.get(i).intValue();
}
return ret;
}
public static void addStock()
{
stockList.add(20);
stockList.add(30);
}
public static void main(final String[] args)
{
addStock();
final int[] stockListFINAL = convertIntegers(stockList);
for (int i = 0; i < stockListFINAL.length; i++)
{
System.out.println(String.format(" For location %d in the int[] the value is %d", i, stockListFINAL[i]));
}
}
}