从ArrayList访问对象的详细信息

时间:2015-02-27 21:13:44

标签: java object arraylist

编辑:如果第一次写得非常糟糕,我道歉。

我似乎遇到的问题是main方法没有get()我在ArrayList中的对象,而是看起来像是在取一个字符串。如果我在main方法中使用getArr方法,则ArrayList似乎表现良好,get()为我提供了存储的对象。

如果我在粒子束类中使用get()方法,结果似乎是字母和数字的随机组合,前面是@符号。

如果我使用System.out.println(chargedBunch.getArr().get(1));,则输出为字符串。

当前输出:     PhysicsVector@7852e922 The average velocity is: PhysicsVector@4e25154f

mass 1.67E-27 Position: -0.7741333834277127 0.5957089157918354 -0.8629478031050868 Velocity: 0.01 0.0 0.0 Acceleration: 0.0 0.0 0.0

主要方法:

public class ChargedParticleSimulationV2 {

public static void main(String[] args){


    double pSpeed = 1.0 * 10e-3;                                                  // initial speed of particle in ms^-1
    PhysicsVector pDirection = new PhysicsVector(1, 0, 0);                      // initial direction in cartesian co-ordinates

    double magnetic_field_magnitude = 1 * 1e-7;                                 // magnitude of magnetic field
    PhysicsVector magnetic_field_direction = new PhysicsVector(0, 0, -1);       // direction of magnetic field

    double time = 0.0;                                                          // set simulation start time (t=0)
    double timeStep = 1 * 10e-7;                                                // determines how frequently the particle is incrememnted (***AFFECTS ACCURACY***)

    PhysicsVector magnetic_field = PhysicsVector.scale(magnetic_field_magnitude, magnetic_field_direction.getUnitVector());

    PhysicsVector electric_field = new PhysicsVector(0, 0, 0);                  // This program only explores the effects of a
    // magnetic field, hence E=0
    EMField field = new EMField(electric_field, magnetic_field);

//******everything works fine till here
    ParticleBunch chargedBunch = new ParticleBunch(-1, 1, "proton", 100);
    chargedBunch.setBunchVelocity(PhysicsVector.scale(pSpeed, pDirection));

    System.out.println("The average velocity is: " + chargedBunch.getAvgVelocity().toString());

System.out.println(chargedBunch.getArr().get(1));
}
}

PhysicsVector类:

public class PhysicsVector {

// Fix the dimension of the array representing the vectors
private static final int    vectorSize          = 3;

// In this case we have a three dimensional vector, the x component is [0] with y[1] and z [2]
private double[]            vectorComponents    = new double[vectorSize];

/**
 * Default contructor that creates a PhysicsVector with zero magnitude
 **/
public PhysicsVector() {
    for (int i = 0; i < vectorComponents.length; i++) {
        vectorComponents[i] = 0.;
    }
}

    public PhysicsVector(double x, double y, double z) {
    vectorComponents[0] = x;
    vectorComponents[1] = y;
    vectorComponents[2] = z;
}    


    public void print() {
    String text = this.returnString();
    System.out.println(text);
}
    public static PhysicsVector add(PhysicsVector v, PhysicsVector u) {
    PhysicsVector sum = new PhysicsVector(v);
    sum.increaseBy(u);
    return sum;
}

    public void increaseBy(PhysicsVector v) {
    for (int i = 0; i < vectorComponents.length; i++) {
        vectorComponents[i] += v.vectorComponents[i];
    }
}

EM Field类:

public class EMField {

protected PhysicsVector electric;   // electric field strength
protected PhysicsVector magnetic;   // magnetic flux density

/**
 * Default constructor. Set data members to zero.
 *
 */
public EMField() {
    electric = new PhysicsVector();
    magnetic = new PhysicsVector();
}

/**
 * Constructor with two inputs - the electric field strength and magnetic flux density
 *
 * @param electricIn The electric field strength
 * @param magneticIn The magnetic flux density
 */
public EMField(PhysicsVector electricIn, PhysicsVector magneticIn) {
    electric = new PhysicsVector(electricIn);
    magnetic = new PhysicsVector(magneticIn);
}

粒子类:

public class Particle {

protected double    mass;   // the mass of the particle
protected PhysicsVector position, velocity, acceleration;

    public Particle() {
    mass = 0;
    position = new PhysicsVector();
    velocity = new PhysicsVector();
    acceleration = new PhysicsVector();
}

ChargedParticle类:

public class ChargedParticle extends Particle {

private double  charge; // charge of the particle

    public ChargedParticle(String nameParticle) {

    super();
    charge = 0;
    if (nameParticle.equalsIgnoreCase("Proton")) {
        charge = 1.602192e-19;
        mass = 1.67e-27;
    }
    else if (nameParticle.equalsIgnoreCase("Electron")) {
        charge = -1.602192e-19;
        mass = 9.11e-31;
    }

}

粒子束类:

public class ParticleBunch {

protected ArrayList<ChargedParticle>    chargedBunch;

public ParticleBunch(int min, int max, String particleName, int particleNumber) {

    chargedBunch = new ArrayList<ChargedParticle>();

    for (int particlei = 0; particlei < particleNumber; particlei++) {

        ChargedParticle chargedParticle = new ChargedParticle(particleName);
        PhysicsVector randOrigin = new PhysicsVector(randomWithinRange(min, max), randomWithinRange(min, max), randomWithinRange(min, max));
        chargedParticle.setPosition(randOrigin);

        chargedBunch.add(particlei, chargedParticle);
    }
}

public static double randomWithinRange(double min, double max) {

    double random = new Random().nextDouble();
    double result = min + (random * (max - min));
    return result;
}

    public ArrayList<ChargedParticle> getArr() {

    return chargedBunch;
}

    public PhysicsVector getAvgVelocity() {

    PhysicsVector avgVelocity = new PhysicsVector();

    System.out.println(chargedBunch.get(1).getPosition().toString());


    for (int particlei = 0; particlei < chargedBunch.size(); particlei++) {

        avgVelocity.increaseBy(chargedBunch.get(particlei).getVelocity());
    }

    avgVelocity.scale(1 / chargedBunch.size());

    return avgVelocity;

}

1 个答案:

答案 0 :(得分:1)

您没有比较相同的输出:

System.out.println("Average Position: " + av.toString() ...

在这里,您可以调用toString()对象上的PhysicVector方法。如果未明确定义,则Object类中将有一个默认实现,它将返回类名和@。 在另一段代码中:

chargedBunch.getArr().get(1).getPosition().print();

您调用了一个您确定定义的print()方法,该方法会执行一些System.out.print()内容。

您必须为toString()定义特定的PhysicVector,并在要打印某些日志时使用它。