【问题标题】:I need to convert my ArrayList into Arrays我需要将我的 ArrayList 转换为 Arrays
【发布时间】:2016-01-20 17:59:26
【问题描述】:
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,但我不太喜欢如何做到这一点。我是初学者,所以我仍在学习如何正确使用数组。我对这两者感到困惑,因为它们与我相似,我不知道如何与它们一起工作。任何帮助将不胜感激

【问题讨论】:

标签: java arrays arraylist


【解决方案1】:
Cylinder[] array = new Cylinder[cyll.size()];
cyll.toArray(array);

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 2012-01-09
    • 2010-10-21
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    相关资源
    最近更新 更多