繁体   English   中英

尝试将整数数组列表转换为原始类型int []

[英]Trying to convert an arraylist of integers into primitive type int[]

我有一个名为stockList的整数stockList

我想要从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但是返回空。

这里发生了什么?

您没有调用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]

您从未将库存添加到列表中。

如果在下面的类中运行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]));
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM