簡體   English   中英

訪問子類中的超類arraylist

[英]Accessing a superclass arraylist in a subclass

希望我能按預期描述我的問題:我有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)),它會發出一個錯誤,指出“找不到臨時符號”。 請幫助我...提前謝謝。 更新我做了一個調整“ ArrayList allLogsText = new ArrayList(JavaApplication65.temp);” 我將其設置為JavaApplication65.temp,它似乎可以正常工作。 但是行得通嗎? 我的目的是編寫某種日記,並且由於這一行代碼不正確,因此嘗試獲取已寫入LogList.txt的每個條目**

范圍很重要。

您正在訪問的List位於main()方法的本地。 您只能在main()方法中訪問它。

將其移到頂級,該頂級應該是實例成員(並提供一個getter來返回該實例)以進行訪問。

您的臨時列表是“ 方法本地” ,如果您想在子類中使用屬​​性,請在您的Super類的類級別聲明這些屬性。 像這樣:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM