我的問題是非常基本的,但我似乎無法在別人問的問題中找到任何真正的幫助......他們的代碼太複雜了。從一個文件導入ArrayList到另一個
基本上,我在一個java文件中創建了一個ArrayList,並且我試圖將它導入到另一個java文件。我可以這樣做嗎?或者我只需要重建它我的第二個文件?
在此先感謝!
這是創建ohnode文件:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class trimgroup {
public static void main(String[] args) throws IOException, StringIndexOutOfBoundsException, NumberFormatException{
Scanner scan = new Scanner(new File("VspAircraft_calculix_geom.dat"));
Scanner scan1 = new Scanner(new File("VspAircraft_calculix_geom.dat"));
Scanner scan2 = new Scanner(new File("VspAircraft_calculix_geom.dat"));
FileWriter file = new FileWriter(new File("trimgroup.dat"));
double ymax=0;
double ymin1=0;
double ymin=0;
double ymineq=0;
double xmax=0;
double xmin1=0;
double xmin=0;
double xmineq=0;
double ymaxb=0;
double ymin1b=0;
double yminb=0;
double ymineqb=0;
double xmaxb=0;
double xmin1b=0;
double xminb=0;
double xmineqb=0;
ArrayList<Double> a = new ArrayList<Double>(); //y values of all the nodes
ArrayList<Double> b = new ArrayList<Double>(); //x values of all the nodes
ArrayList<Integer> node = new ArrayList<Integer>(); //all the nodes on the wing
ArrayList<Integer> nodeb = new ArrayList<Integer>();
ArrayList<Integer> ohnode = new ArrayList<Integer>();
//Front Spar
while(scan.hasNextLine()){
String line = scan.nextLine();
String [] token = line.split("[,|\\r?\\n]");
if (token.length==2){
for(int i=0;i<token[1].length()-11;i++) {
if (token[1].substring(i,i+12).equals(" NSET=Nspar1")){
line=scan.nextLine(); // first skip any input that matches the delimiter pattern, and then attempt to return the next token
while(scan.hasNextLine()){
String [] token2= line.split("[,|\\r?\\n]");
if (token2[0].equals("")){
break;
}else{
String [] token3= line.split("[,|\\r?\\n]");
double token2d=Double.parseDouble(token3[2]);
double token1d=Double.parseDouble(token3[1]);
int token0d=Integer.parseInt(token3[0]);
a.add(token2d);
node.add(token0d);
//Find the max/min
if (token2d>ymax){
ymax=Double.parseDouble(token3[2]);
}else{
ymin1=Double.parseDouble(token3[2]);}
if (1/ymin1>ymineq){
ymin=token2d;
ymineq=1/token2d;}
if (token1d>xmax){
xmax=Double.parseDouble(token3[1]);
}else{
xmin1=Double.parseDouble(token3[1]);}
if (1/xmin1>xmineq){
xmin=token1d;
xmineq=1/token1d;}
line=scan.nextLine();
}
}
}
}
}
}
//Back Spar
while(scan1.hasNextLine()){
String line = scan1.nextLine();
String [] token = line.split("[,|\\r?\\n]");
if (token.length==2){
for(int i=0;i<token[1].length()-11;i++) {
if (token[1].substring(i,i+12).equals(" NSET=Nspar2")){
line=scan1.nextLine();
while(scan1.hasNextLine()){
String [] token2= line.split("[,|\\r?\\n]");
if (token2[0].equals("")){
break;
}else{
String [] token3= line.split("[,|\\r?\\n]");
double token2d=Double.parseDouble(token3[2]);
double token1d=Double.parseDouble(token3[1]);
int token0d=Integer.parseInt(token3[0]);
a.add(token2d);
node.add(token0d);
if (token2d>ymaxb){
ymaxb=Double.parseDouble(token3[2]);
}else{
ymin1b=Double.parseDouble(token3[2]);}
if (1/ymin1b>ymineqb){
yminb=token2d;
ymineqb=1/token2d;}
if (token1d>xmaxb){
xmaxb=Double.parseDouble(token3[1]);
}else{
xmin1b=Double.parseDouble(token3[1]);}
if (1/xmin1b>xmineqb){
xminb=token1d;
xmineqb=1/token1d;}
line=scan1.nextLine();
}
}
}
}
}
}
while(scan2.hasNextLine()){
String line2 = scan2.nextLine();
String [] token2 = line2.split("[,|\\r?\\n]");
if (token2.length==2){
if (token2[1].equals(" NSET=Nupperskin")){
line2=scan2.nextLine();
while (scan2.hasNextLine()){
String [] token2b = line2.split("[,|\\r?\\n]");
if (token2b[0].equals("")){
break;
}else{
String [] token3b = line2.split("[,|\\r?\\n]");
int token2bi=Integer.parseInt(token3b[0]);
double token2by=Double.parseDouble(token3b[2]);
double token2bx=Double.parseDouble(token3b[1]);
if (token2bx<(xmax-xmin)/(ymax-ymin)*token2by+xmin){
ohnode.add(token2bi);
}
if (token2bx>(xmaxb-xminb)/(ymaxb-yminb)*token2by+xminb){
ohnode.add(token2bi);
}
line2=scan2.nextLine();
}
}
}
}
if (token2.length==2){
if (token2[1].equals(" NSET=Nlowerskin")){
line2=scan2.nextLine();
while (scan2.hasNextLine()){
String [] token2b = line2.split("[,|\\r?\\n]");
if (token2b[0].equals("")){
break;
}else{
String [] token3b = line2.split("[,|\\r?\\n]");
int token2bi=Integer.parseInt(token3b[0]);
double token2by=Double.parseDouble(token3b[2]);
double token2bx=Double.parseDouble(token3b[1]);
if (token2bx<(xmax-xmin)/(ymax-ymin)*token2by+xmin){
ohnode.add(token2bi);
}
if (token2bx>(xmaxb-xminb)/(ymaxb-yminb)*token2by+xminb){
ohnode.add(token2bi);
}
line2=scan2.nextLine();
}
}
}
}
}
System.out.println(nodeb.size());
System.out.println(node.size());
System.out.println(ohnode.size());
for (int u=0;u<ohnode.size();u++)
file.write(ohnode.get(u)+"\r\n");
file.close();
}}
這是我想要使用ArrayList的ohnode在文本文件中:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
public class TrimBox {
public static void main(String[] args) throws IOException, StringIndexOutOfBoundsException, NumberFormatException{
Scanner scan = new Scanner(new File("VspAircraft_calculix_geom.dat"));
Scanner scan2 = new Scanner(new File("trimgroup.dat"));
FileWriter file = new FileWriter(new File("trimbox_nodes.dat"));
ArrayList<Integer> node = new ArrayList<Integer>();
//ALL the nodes of the entire wing
while (scan.hasNextLine()){
String line = scan.nextLine();
String [] token= line.split(",|\\r?\\n]");
if (token.length==2){
for(int i=0;i<token[1].length()-11;i++) {
if (token[1].substring(i,i+12).equals(" NSET=Nspar0")){
//Ask user to change the number after Nspar in modelcenter to denote which spar to apply load
line=scan.nextLine();
while(scan.hasNextLine()){
String [] token2= line.split("[,|\\r?\\n]");
if (token2[0].equals("")){
break;
}else{
String [] token3= line.split("[,|\\r?\\n]");
//System.out.println(token3[2]);
int token0d=Integer.parseInt(token3[0]);
node.add(token0d); //Add the node tokens to array node
//Order of the tokens from VSPAircraft_calculix_geom.data
//node, x value, y value, z value
}
}
}
}
}
}
}
}
/** We want to import the arraylist from trimgroup.dat (nodeb or ohnode?)
* Can we also import arraylist node from trimgroup.dat?
* -If nodes from VSP==nodeb (/ohnode?), skip, if not, add token to new array
* -Write it to a new file
*
* Probably can use some function of java.util.ArrayList to see if ArrayLists are equal
*
* Why do we have ArrayLists instead of Arrays?
*/
在這種情況下,「import」是什麼意思? – 2012-03-03 22:13:43
按文件到文件你是指從類到文件或文本文件到文本文件? – 2012-03-03 22:16:52
我在猜測你已經有了ArrayList在一個對象中,並希望另一個對象能夠使用它。如果是這樣,只需通過構造函數參數或「setter」方法參數傳遞給第二個對象 - 就像餡餅一樣簡單。但請不要讓我們猜測。請澄清你的問題,以便我們都可以談論同樣的事情。 – 2012-03-03 22:19:17