0
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;

    }
}

I need to convert my ArrayList into Arrays but i am not too fond on how to do this. I am a beginner so I am still learning how to use arrays correctly. I get confused with the two because they're similar to me and I can't figure out exactly how to work with them. Any help would be appreciated

2
  • 1
    List interface has a method called toArray Commented Oct 21, 2015 at 16:21
  • 4
    Possible duplicate of From Arraylist to Array Commented Oct 21, 2015 at 16:22

1 Answer 1

3
Cylinder[] array = new Cylinder[cyll.size()];
cyll.toArray(array);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.