将ArrayList从一个文件导入另一个文件

时间:2012-03-03 22:11:38

标签: java 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?  
 */

1 个答案:

答案 0 :(得分:1)

很难从你的问题中判断出你想要做什么,所以我会做出一些猜测。

如果要将ArrayList从一个对象传递到另一个对象,有几种方法可以做到这一点,但它取决于您希望对传输进行“负责”的对象。


1)对象A可以创建一个对象B,并在构造函数中传递ArrayList:

new A(myArrayList);

2)如果两个对象都已存在,则对象A可以调用对象B中的setter方法:

B.setList(myArrayList);

这假设A有一个对B的引用,并且你在B中创建了一个setter方法!像这样设置对象不一定是好设计,但这是另一个问题。


3)对象B可以使用getter方法从A获取对象(或直接访问它,具体取决于它是否可见,例如publicprivate) :

ArrayList myList = A.getList();

ArrayList myList = A.myArrayList;

同样,这假设B具有对A的引用,并且在第一种情况下,您已在A中创建了一个getter方法!


最后请注意import在Java中具有特定含义 - 请参阅刚刚发布的代码顶部的导入,例如:

import java.util.ArrayList;

导入ArrayList类与将实际的ArrayList实例从一个对象或类传递到另一个对象或类不同。


更新:现在您已发布了代码 - 请参阅 Hovercraft Full Of Eels 中的评论。您基本上有两个单独的脚本,这些脚本不是设计为在同一JVM中同时一起运行。因此,它们不能在它们之间传递ArrayList,除非它们将它写入共享文件,或者通过网络传递它,这是另一个问题。

相关问题