0

adjcloseクラスにこのコードがあり、理想的には配列リスト の値を別のクラスにコピーして、元のデータを保持しながらさらに処理したいと考えています。

println配列が取り込まれているため、ステートメントの前に、ステートメントの値が arraylist に取り込まれていることがわかりますreturn

次に、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();


    }



}

計算に使用したい別のパッケージに別のクラスがあり、元のデータ要素を元の配列に保持し、新しい計算データを 2 番目のクラスの配列に保存します。他のクラスの開始コードは次のとおりです。data.clone() で複製されたデータを取得するにはどうすればよいですか?

package PortfolioDesign;

/**
 *
 * @author RSLOMA
 */


public class MonthlyReturns {


        }
4

3 に答える 3

1

結果が複数回使用される場合、メソッドを次のように分割します。

  • データ収集方法
  • データ取得方法

次に、データ コレクターはクラスのメンバーを入力します。

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

public void  fillAdjClose(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<Double>();
    System.out.print("Opening URL: ");
    System.out.print(baseUrl);
    System.out.println(" ");
    int counter = 0;
    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;
    }

    // update the date once the read is done
    data = adjclose;
}

public ArrayList<Double> getAdjClose() {
    return (ArrayList<Double>) data.clone();
}

必要に応じて getAdjClose() を呼び出すことができ、最後に読み取ったデータのコピーを常に取得できます。

要素のコピーも必要でない限り、いつでも ArrayList に clone() を使用できます。不変の Double を使用しているため、要素をコピーする必要はありません。

于 2012-05-24T14:58:22.350 に答える
1

配列のクローンを作成してクローンを返す最初のクラスにメソッドを用意し、2 番目のクラスからそのメソッドを呼び出します。

于 2012-05-24T14:37:07.480 に答える
0

adjclosesay from Class Atoの値を公開したい場合、Class Bそれを含むクラスにClassAは、理想的には my と呼ばれる getter メソッドが必要ClassBです。

元のデータを保存するにadjCloseは、元のリスト自体ではなく、コピーを公開する必要があります。また、浅いクローンではなく、深いクローンを作成する必要があることに注意してください。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public classA {
   private List<Double> adjclose = null;

   public List<Double> getAdjClose(){
     List<Double> returnValue = new ArrayList<Double>();
     if(adjclose != null) {
        returnValue.addAll(adjclose); // this will ensure that if caller makes any changes to adjClose, copy of classA remains intact.
        return returnValue;
     }
     return Collections.emptyList();
   }    
}

次に、呼び出し側クラス ClassB が ClassA のインスタンスを作成し、それに対して getAdjClose() を呼び出します。

import java.util.List;
public class ClassB {
   public static void main(String[] args){
      ClassA classA = new ClassA();
      List<Double> list = classA.getAdjClose();
      System.out.println(list);
      list.set(0, new Double(11));
      System.out.println(list);
      System.out.println(classA.getAdjClose());
   }
}
于 2012-05-24T14:38:31.403 に答える