我需要将ArrayList转换为Arrays

我需要将ArrayList转换为Arrays

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;

public class CylinderList2 {
    private String listName = "";
    private ArrayList<Cylinder> cyll =
            new ArrayList<Cylinder>();

    /**
     * @param cyllIn     Represents list of cylinder objects.
     * @param listNameIn Represents name of list.
     */
    public CylinderList2(String listNameIn,
                         ArrayList<Cylinder> cyllIn) {
        listName = listNameIn;
        cyll = cyllIn;
    }

    /**
     * @return A String representing the name of the list. Returns a string that represents name of
     * the list.
     */
    public String getName() {
        return listName;
    }

    /**
     * @return The number of Cylinder objects. Returns the number of cylinder objects.
     */
    public int numberOfCylinders() {
        return cyll.size();
    }

    /**
     * @return Total area. Returns total area of cylinder objects.
     */
    public double totalArea() {
        double tArea = 0;
        int index = 0;

        if (cyll.size() == 0) {
            return 0;
        }

        while (index < cyll.size()) {
            tArea += cyll.get(index).area();

            index++;
        }
        return tArea;
    }

    /**
     * @return Displays volume when method is called. Returns total volume of cylinder objects.
     */
    public double totalVolume() {
        double tVolume = 0;
        int index = 0;

        if (cyll.size() == 0) {
            return 0;
        }

        while (index < cyll.size()) {
            tVolume += cyll.get(index).volume();

            index++;
        }
        return tVolume;
    }

    /**
     * @return Displays height when method is called. Returns double representing total height of
     * all cylinder objects.
     */
    public double totalHeight()

    {

        double tHeight = 0;

        int index = 0;

        if (cyll.size() == 0)

        {

            return 0;

        }

        while (index < cyll.size())

        {

            tHeight += cyll.get(index).getHeight();


            index++;

        }

        return tHeight;

    }
}


我需要将ArrayList转换为Arrays,但我不太喜欢如何执行此操作。我是一个初学者,所以我仍在学习如何正确使用数组。我对这两者感到困惑,因为它们与我相似,我无法确切地知道如何使用它们。任何帮助,将不胜感激

最佳答案

Cylinder[] array = new Cylinder[cyll.size()];
cyll.toArray(array);

09-05 02:22