无法从java中的其他类访问arraylist类中的方法

时间:2016-08-07 16:23:57

标签: java arraylist

我的简单项目中有2个类文件 - 抱歉另一个新手在这里!

但是我在最后一部分遇到编译错误,我试图从项目的文件中打印希望存储的配置设置,这将在整个项目中引用。 该文件只是像这样的值的行' ButtonConfig,8,V,NULL,bunny,mpg'

我基本上希望能够使用这个arraylist的内容来动态设置Raspberry pi GPO引脚的配置,即上面的值按钮连接到GPO引脚8将播放视频(V)"< ; ..其他值...> _bunny.mpg"

非常感谢任何帮助 - 告诉我为什么我无法访问getExtension方法会很好!

第一个java文件的内容是 -

package bpunit;

public class ButtonConfig {
   private String keyword;
   private String gponumber;
   private String buttontype;
   private String language;
   private String filename;
   private String extension;

   public String getKeyword() {
      return keyword;
   }
   public void setKeyword(String keyword) {
      this.keyword = keyword;
   }

   ...............

   public String getExtension() {
     return extension;
   }

   public void setExtension(String extension) {
     this.extension = extension;
   }

}

第二个包含这个 -

package bpunit;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Read_ini {
    public void Read_ini_toObject() 
    {
    String csvFileToRead = "configs/BPUnit.properties";
    BufferedReader br = null;
    String line;
    String splitBy = ",";
    List buttonList = new ArrayList();

    try {

        br = new BufferedReader(new FileReader(csvFileToRead));
        while ((line = br.readLine()) != null) {

            // split on comma(',')
            String[] buttonconfig = line.split(splitBy);

            // create button object to store values
            ButtonConfig buttonObject = new ButtonConfig();

            // add values from csv to car object
            buttonObject.setKeyword(buttonconfig[0]);
            buttonObject.setGponumber(buttonconfig[1]);
            buttonObject.setButtontype(buttonconfig[2]);
            buttonObject.setLanguage(buttonconfig[3]);
            buttonObject.setFilename(buttonconfig[4]);
            buttonObject.setExtension(buttonconfig[5]);
            // adding button object to a list
            buttonList.add(buttonObject);

        }
        // print values stored in buttonList
        printButtonList(buttonList);

        } catch (FileNotFoundException e) {
           System.out.print(e);
        } catch (IOException e) {
           System.out.print(e);
        } finally {
           if (br != null) {
               try {
                   br.close();
               } catch (IOException e) {
                   System.out.print(e);
               }
           }
       }
    }

    public void printButtonList(List buttonListToPrint) {
        for (int i = 0; i < buttonListToPrint.size(); i++) {            

        // THE LINE BELOW FAILS - getExtension() does not exist 
        // and all other attempts give me pointer references 
        //instead of the text //

            System.out.println(buttonListToPrint.get(i).getExtension());        

           }
      } 

   } 

2 个答案:

答案 0 :(得分:1)

您必须将参数化类型ButtonConfig添加到ArrayList。它最终为List<ButtonConfig>,而不仅仅是List

package bpunit;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Read_ini {
    public void Read_ini_toObject() 
    {
    String csvFileToRead = "configs/BPUnit.properties";
    BufferedReader br = null;
    String line;
    String splitBy = ",";
    List<ButtonConfig> buttonList = new ArrayList<ButtonConfig>();

    try {

        br = new BufferedReader(new FileReader(csvFileToRead));
        while ((line = br.readLine()) != null) {

            // split on comma(',')
            String[] buttonconfig = line.split(splitBy);

            // create button object to store values
            ButtonConfig buttonObject = new ButtonConfig();

            // add values from csv to car object
            buttonObject.setKeyword(buttonconfig[0]);
            buttonObject.setGponumber(buttonconfig[1]);
            buttonObject.setButtontype(buttonconfig[2]);
            buttonObject.setLanguage(buttonconfig[3]);
            buttonObject.setFilename(buttonconfig[4]);
            buttonObject.setExtension(buttonconfig[5]);
            // adding button object to a list
            buttonList.add(buttonObject);

        }
        // print values stored in buttonList
        printButtonList(buttonList);

        } catch (FileNotFoundException e) {
           System.out.print(e);
        } catch (IOException e) {
           System.out.print(e);
        } finally {
           if (br != null) {
               try {
                   br.close();
               } catch (IOException e) {
                   System.out.print(e);
               }
           }
       }
    }

    public void printButtonList(List<ButtonConfig> buttonListToPrint) {
        for (int i = 0; i < buttonListToPrint.size(); i++) {            

        // THE LINE BELOW FAILS - getExtension() does not exist 
        // and all other attempts give me pointer references 
        //instead of the text //

            System.out.println(buttonListToPrint.get(i).getExtension());        

           }
      } 

   } 

答案 1 :(得分:-1)

编译失败的原因是因为当您向ArrayList添加对象时,它会被上传为类Object的对象。现在,当您提取它时,您只需将其强制转换回原始类型即可。所以你要做的就是:

public void printButtonList(List buttonListToPrint) {
        for (int i = 0; i < buttonListToPrint.size(); i++) {            

        // THE LINE BELOW FAILS - getExtension() does not exist 
        // and all other attempts give me pointer references 
        //instead of the text 
            ButtonConfig buttonObject =(ButtonConfig)buttonListToPrint.get(i);
            System.out.println(buttonObject.getExtension());        

           }
      } 

或者如上面的评论和解答中所述,您可以使用generics并创建类型为ButtonConfig的列表

public void Read_ini_toObject() 
    {
    String csvFileToRead = "configs/BPUnit.properties";
    BufferedReader br = null;
    String line;
    String splitBy = ",";
    List<ButtonConfig> buttonList = new ArrayList<ButtonConfig>();

并在函数printButtonList

中传递它
public void printButtonList(List<ButtonConfig> buttonListToPrint) {
        for (int i = 0; i < buttonListToPrint.size(); i++) {            

        // THE LINE BELOW FAILS - getExtension() does not exist 
        // and all other attempts give me pointer references 
        //instead of the text                
System.out.println(buttonListToPrint.get(i).getExtension());  



           }
      }