gpt4 book ai didi

java - 从一个类的数组中获取另一类的值

转载 作者:行者123 更新时间:2023-12-02 07:46:36 24 4
gpt4 key购买 nike

我在一个类中包含此代码,并且希望理想地将 adjclose 数组列表中的值复制到另一个类以进行进一步处理,同时保留原始数据。

我可以看到,在填充数组时,数组列表中填充了 return 语句之前的 println 语句的值。

然后,main 方法会迭代 arraylist,再次显示 arraylist adjclose 中每个元素的值。

如何从另一个类获取 adjclose 数组列表,以便将它们复制到新的数组列表中以进一步处理?

public ArrayList<Double> getadjClose(String symbol) {
String baseUrl = "http://ichart.finance.yahoo.com/table.csv?ignore=.csv";
baseUrl += "&s=" + symbol;
baseUrl += "&a=" + startMonth;
baseUrl += "&b=" + startDay;
baseUrl += "&c=" + startYear;
baseUrl += "&d=" + endMonth;
baseUrl += "&e=" + endDay;
baseUrl += "&f=" + endYear;
baseUrl += "&g=" + freq;
URL url;
ArrayList<Double> adjclose = new ArrayList<Double>();
System.out.print("Opening URL: ");
System.out.print(baseUrl);
System.out.println(" ");
int counter = 0;
try {
url = new URL(baseUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream()));
in.readLine(); // Forward Header
while (true) {
String thisLine = in.readLine();
if (thisLine == null) {
break;
}
String[] separatedLine = thisLine.split("[,X]"); // split by commas
adjclose.add(Double.parseDouble(separatedLine[6]));
System.out.println(adjclose.get(counter) + " " + counter);
counter = counter + 1;
}
return adjclose;
} catch (IOException e) {
return null;
}
}

我现在已经对第一个类中的代码进行了更改,如下所示。

package yahooapi;

/**
*
* @author RSLOMA
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;

public class YahooAPI {


int startMonth;
int startDay;
int startYear;

int endMonth;
int endDay;
int endYear;
int TodayDate;

String freq;

public ArrayList<Double> data = new ArrayList<>();


public ArrayList<Double> getAdjClose(String symbol) throws IOException {
String baseUrl = "http://ichart.finance.yahoo.com/table.csv?ignore=.csv";
baseUrl += "&s=" + symbol;
baseUrl += "&a=" + startMonth;
baseUrl += "&b=" + startDay;
baseUrl += "&c=" + startYear;
baseUrl += "&d=" + endMonth;
baseUrl += "&e=" + endDay;
baseUrl += "&f=" + endYear;
baseUrl += "&g=" + freq;

URL url;

// use a local variable

ArrayList<Double> adjclose = new ArrayList<>();

System.out.print("Opening URL: ");
System.out.print(baseUrl);
System.out.println(" ");

int counter = 0;

try {
url = new URL(baseUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

in.readLine(); //Forward Header

while (true){
String thisLine = in.readLine();
if (thisLine == null){
break;
}
String[] separatedLine = thisLine.split("[,X]"); // split by commas

adjclose.add(Double.parseDouble(separatedLine[6]));
System.out.println(adjclose.get(counter) + " " + counter);

// update the data once the read is done

data = adjclose;
System.out.println(data.get(counter));

counter = counter + 1;

}


return adjclose;

} catch (IOException e) {
return null;
}
}


public static void main(String args[]) throws IOException{
YahooAPI y = new YahooAPI();
Calendar cal = Calendar.getInstance();

y.startDay = 1;
y.startMonth = cal.get(Calendar.MONTH) - 1; //0 is jan, so 2 is march
y.startYear = cal.get(Calendar.YEAR) - 3;
System.out.println("Day: " + y.startMonth);
System.out.println("Day: " + y.startDay);
System.out.println("Day: " + y.startYear);

y.endDay = cal.get(Calendar.DATE) - (cal.get(Calendar.DATE) - 1);
y.endMonth = cal.get(Calendar.MONTH); //0 is jan, so 2 is march
y.endYear = cal.get(Calendar.YEAR);

y.freq = "m"; // d for daily frequency, w for weekly, m for monthly

ArrayList<Double> adjclose = y.getAdjClose("^GSPC");

//Iterator<Double> iter = adjclose.iterator();

//System.out.println("Returned Adjusted Close Values:");
//while (iter.hasNext()){
//System.out.println(iter.next());


int ArrayLngth = adjclose.size();

System.out.print("Array length = " + ArrayLngth + " ");
}

public ArrayList<Double> getAdjClose() {

for (int counter = 0; counter<data.size(); counter++) {

System.out.println(data.get(counter) + " " + counter);

}



return (ArrayList<Double>) data.clone();


}



}

我在另一个包中有另一个类,我想用它来进行计算,保留原始数组中的原始数据元素,并将新计算的数据保存在第二个类的数组中。另一个类的起始代码如下。如何调用获取data.clone()中克隆的数据?

package PortfolioDesign;

/**
*
* @author RSLOMA
*/


public class MonthlyReturns {


}

最佳答案

在第一个类中有一个方法,该方法将创建数组的克隆并返回该克隆,然后从第二个类中调用该方法。

关于java - 从一个类的数组中获取另一类的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10739472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com