2015-10-21 3 views
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; 

    } 
} 

Мне нужно преобразовать массив ArrayList в массивы, но я не слишком люблю, как это сделать. Я новичок, поэтому я все еще изучаю, как правильно использовать массивы. Я смущаюсь с ними, потому что они похожи на меня, и я не могу понять, как с ними работать. Любая помощь будет оцененаМне нужно преобразовать свой массив ArrayList в массивы

+1

'list' интерфейс имеет метод, называемый' toArray' –

+4

Возможные дубликат [От Arraylist к массиву] (http://stackoverflow.com/questions/7969023/from-arraylist-to -array) –

ответ

3
Cylinder[] array = new Cylinder[cyll.size()]; 
cyll.toArray(array); 
Смежные вопросы