【发布时间】:2014-08-08 06:45:44
【问题描述】:
希望我能按预期描述我的问题:我有 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 的每个条目**
【问题讨论】:
-
你想在这里实现什么??为什么要将温度放在 main 中?你在几个概念上是错误的。你能描述一下你的目标是什么吗?不要混合使用静态成员和实例成员。
标签: java inheritance arraylist subclass superclass