访问子类中的超类arraylist

时间:2014-08-08 06:45:45

标签: java inheritance arraylist subclass superclass

希望我可以按照预期描述我的问题:我有2个班级; JavaApplication65(请不要质疑名称:D)和JavaApplication65,Logs的子类。现在我在JavaApplication65中有一个名为temp的ArrayList,我想将它的值复制到Logs中名为allLogsText的Arraylist中。我该怎么办?

package javaapplication65;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author MertKarakas
 */
public class JavaApplication65 {
/**
 * @param args the command line arguments
 */
Logs importer = new Logs();
private String nameOfLog;
private String wholeLog;
static ArrayList<String> temp = new ArrayList<String>();

/**
 *
 * @param nameOfLog1
 * @param wholeLog1
 */
public JavaApplication65(String nameOfLog1, String wholeLog1){
    this.nameOfLog = nameOfLog1;
    this.wholeLog = wholeLog1;
}
public JavaApplication65(){}
public static void main(String[] args) throws FileNotFoundException{
    System.out.println("Data Log V1. @author MertKarakas");
    Scanner s = new Scanner(new File("/Users/mertkarakas/Desktop/Logs/LogList.txt"));
    while (s.hasNext()){
        temp.add(s.next());
        }
    s.close();
    new Logs().menuOpt();

}
}


public class Logs {
Scanner console = new Scanner(System.in);
ArrayList<JavaApplication65> allLogs = new ArrayList<JavaApplication65>();
ArrayList<String> allLogsText = new ArrayList<String>(JavaApplication65.temp);
private String name;
public void Logs(){
    this.name = "";
}

public void menuOpt(){
    System.out.println("Menu: Write a log[log], See entered logs[see] --> Read an entered log[#], Delete a log[del], Read a specific log[spf]");
    WriteStringToFile2(allLogsText);
    choices();
    WriteStringToFile2(allLogsText);
}

public void choices(){
    String input1 = console.next();
    if ( input1.equalsIgnoreCase("log") ){
        log();
    }else if ( input1.equalsIgnoreCase("see") ){
        getLogs();
        System.out.println("Enter the index of requested log: ");
        int requestedLog = console.nextInt();
        getALog(requestedLog);
    }else if( input1.equalsIgnoreCase("del") ){

    }else if ( input1.equalsIgnoreCase("spf") ){

    }else{
        System.out.println("Wrong input, returnin to menu.");
        menuOpt();
    }
}
public void log(){
    System.out.println("Enter name of the log: ");
    String logName2 = console.next();
    System.out.println("Enter your log: ");
    String wholeLog2 = console.next();
    WriteStringToFile(logName2, wholeLog2);
    JavaApplication65 tempLog = new JavaApplication65(logName2, wholeLog2);
    allLogs.add(allLogs.size(), tempLog);
    allLogsText.add(allLogsText.size(), logName2);
    getLogs();
    System.out.println("Log successfully added.");

}

public List<String> getLogs(){
    return allLogsText.subList(0, allLogsText.size());
}
public String getALog(int k){
    String temp = allLogsText.get(k);
    return temp;
}

public void WriteStringToFile(String logNamerino, String wholeLogarino){
        try {
        String str = wholeLogarino;
        File newTextFile = new File("/Users/mertkarakas/Desktop/Logs/" + logNamerino + ".txt");

        FileWriter fw = new FileWriter(newTextFile);
        fw.write(str);
        fw.close();

    } catch (IOException iox) {
    }
}
    public void WriteStringToFile2(ArrayList<String> list){  
        try {
            for(int k =0; k < list.size(); k++){
                String str = list.get(k);
                File newTextFile = new      File("/Users/mertkarakas/Desktop/Logs/LogList.txt");
                FileWriter fw = new FileWriter(newTextFile);
                fw.write(str);
                fw.close();
            }
        } catch (IOException iox) {
    }
}

}

正如您在代码的最后一行(ArrayList allLogsText = new ArrayList(temp))中所看到的,它为temp发出了“找不到符号”的错误。请帮助我...感谢提前。更新我做了一个调整“ArrayList allLogsText = new ArrayList(JavaApplication65.temp);”我做了JavaApplication65.temp它似乎工作。但它会起作用吗?我的意图是制作一种日记,并使用这行代码生成尝试获取已写入LogList.txt的每个条目**

2 个答案:

答案 0 :(得分:3)

范围很重要。

您正在访问的Listmain()方法的本地。您只能在main()方法中访问它。

将其移至顶级,该级别应为实例成员(并提供一个getter以返回该成员)以进行访问。

答案 1 :(得分:0)

您的临时列表是方法本地,如果您想使用子类中的属性,请在超类的类级别中声明这些属性。像这样:

public class JavaApplication65 {
    ArrayList<String> temp;
    public static void main(String[] args) throws FileNotFoundException{
    JavaApplication65 java= new JavaApplication65();    
    java.temp = new ArrayList<String>();

    }
}

public class Logs extends JavaApplication65 {
     Scanner console = new Scanner(System.in);
     ArrayList<JavaApplication65> allLogs = new ArrayList<JavaApplication65>();
     ArrayList<String> allLogsText = new ArrayList<String>(temp);
}