This discussion is archived
11 Replies Latest reply: Jun 18, 2008 3:34 PM by 807601 RSS

How to overwrite an array?

807601 Newbie
Currently Being Moderated
I am trying to overwrite the values in an array, but I am getting an ArrayIndexOutofBoundsException.
This is how I am attempting to do it:
if ((checkTempEnergy(tempDistance, numIons, Q)) < (checkEnergy(distance, numIons, Q))) {
     for (int i=0; i < numIons; i++) {
          arrayX[i] = tempX;
          arrayY[i] = tempY[i];
          arrayZ[i] = tempZ[i];
          distance[i] = tempDistance[i];
     }
}
The code compiles. Both arrays are the same size and declared the same way. The only difference is the name. Any help would be greatly appreciated, and I will award the 10 DUKE Stars.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 1. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    RommelTJ wrote:
    Both arrays are the same size and declared the same way.
    Both arrays? You have eight arrays.

    Even then, it doesn't matter if they're all the same size, if they all have less than numIons elements.
  • 2. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    edit: read it wrong

    Edited by: redfalconf35 on Jun 18, 2008 5:19 PM
  • 3. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    You've been posting this question over and over and the drill should be familiar to you by now:

    1. Posting a snippet like that isn't really helpful to forum readers. For one thing, the fact that your code has such a trivial error in it makes you an unreliable reporter. Why should we believe what you claim?

    2. Once again, if you report an error you should indicate clearly the line that raises it. Once again you have failed to do this.

    3.. One day you will realize the best way to get help is to write a SSCCE: [http://mindprod.com/jgloss/sscce.html]
  • 4. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    DrLaszloJamf wrote:
    You've been posting this question over and over and the drill should be familiar to you by now:

    1. Posting a snippet like that isn't really helpful to forum readers. For one thing, the fact that your code has such a trivial error in it makes you an unreliable reporter. Why should we believe what you claim?
    Ok, but the program is not complete, repeats itself, and looks awful. It is compilable though.
    public class FormingSalt2 
    {  
         //creates a cube for the salt to form
         public static double volCube (double side) {
              double volume = side*side*side;
         return (volume);
         }
    
         //create a random coordinates within the cube
         public static double randomCoord (double side) {
              double ranCoord = (double) (Math.random() * (side-3.76));
         return (ranCoord);
         }
         
         //check the distances between ions but without double checking and without checking the distances between the same ions.
         public static double[] checkDistance (double arrayX[], double arrayY[], double arrayZ[], double distance[], int numIons) {
              //check the distances
              int l = 0;
              for (int i=1; i<numIons; i++) {
                   for (int j=0; j<i; j++){
                        double dx = arrayX[i] - arrayX[j];
                        double dy = arrayY[i] - arrayY[j];
                        double dz = arrayZ[i] - arrayZ[j];
                        distance[l] = Math.sqrt((dx*dx) + (dy*dy) + (dz*dz));
                        //System.out.println(tempDistance[l]+" is the distance "+ l);
                        l++;
                   }
              }
         return distance;
         }
         
         //check the temporary distances between the randomly moved ions but without double checking and without checking the distances between the same ions.
         public static double[] checkTempDistance (double tempX[], double tempY[], double tempZ[], double tempDistance[], int numIons) {
              //check the distances
              int l = 0;
              for (int i=1; i<numIons; i++) {
                   for (int j=0; j<i; j++){
                        double dx = tempX[i] - tempX[j];
                        double dy = tempY[i] - tempY[j];
                        double dz = tempZ[i] - tempZ[j];
                        tempDistance[l] = Math.sqrt((dx*dx) + (dy*dy) + (dz*dz));
                        //System.out.println(tempDistance[l]+" is the distance "+ l);
                        l++;
                   }
              }
         return tempDistance;
         }
         
         //checks the Total Energy between the ions
         public static double checkEnergy (double distance[], int numIons, double Q[]) {
              final double EPSILON = Math.sqrt(35.6*120), SIGMA = ((2.75+3.41)/2), K1 = 2.30, K2 = 0.0000552;
              double E1=0, E1sum=0, E2=0, E2sum=0;
              for (int i=1; i<numIons; i++) {     
                   for (int j=0; j<i; j++) {     
                        E1 = (K1) * ((Q[j]*Q)/distance[j]);
                        E1sum = E1sum + E1;
                        //System.out.println(E1);
              
                        E2 = (K2)*EPSILON* (Math.pow((SIGMA/distance[j]),12) - Math.pow((SIGMA/distance[j]),6));
                        E2sum = E2sum + E2;
                        //System.out.println(E2);
                        //System.out.println();
                   }
              }     
              //System.out.println(E1sum);
              //System.out.println();
              //System.out.println(E2sum);
              //System.out.println();
              double ETotal = (E1sum + E2sum) * Math.pow(10,-18);
         return ETotal;
         }
         
         //checks the Total Temporary Energy between the randomly placed ions
         public static double checkTempEnergy (double tempDistance[], int numIons, double Q[]) {
              final double EPSILON = Math.sqrt(35.6*120), SIGMA = ((2.75+3.41)/2), K1 = 2.30, K2 = 0.0000552;
              double E1=0, E1sum=0, E2=0, E2sum=0;
              for (int i=1; i<numIons; i++) {     
                   for (int j=0; j<i; j++) {     
                        E1 = (K1) * ((Q[j]*Q[i])/tempDistance[j]);
                        E1sum = E1sum + E1;
                        //System.out.println(E1);
              
                        E2 = (K2)*EPSILON* (Math.pow((SIGMA/tempDistance[j]),12) - Math.pow((SIGMA/tempDistance[j]),6));
                        E2sum = E2sum + E2;
                        //System.out.println(E2);
                        //System.out.println();
                   }
              }     
              //System.out.println(E1sum);
              //System.out.println();
              //System.out.println(E2sum);
              //System.out.println();
              double ETotal = (E1sum + E2sum) * Math.pow(10,-18);
         return ETotal;
         }
         
         
         //MAIN METHOD!
    public static void main(String[] args)
    {
    double side=0;
         
    //Create the Cube and ask the user for how many Ions.
         //Number of Ions must be even for this program to converge/work.
         while (side<1.89){
              System.out.println("Choose length of one side of the Cube in Angstroms:");
              side = StdIn.readDouble();
              if (side<1.89){
              System.out.println("Side must be greater than Van der Waals radius of Argon, which is 1.88A.");
              System.out.println();
              }
         }
         System.out.println("The Volume of the Cube is: "+volCube(side));
         System.out.println();
         System.out.println("How many ions should there be in the Cube? Note: Odds are Na+ and Evens are Cl-");
         int numIons = StdIn.readInt();
         System.out.println();
         
         //Initialize and allocate memory for the Arrays...
         double arrayX[] = new double[numIons];
         double arrayY[] = new double[numIons];
         double arrayZ[] = new double[numIons];
         double tempX[] = new double[numIons];
         double tempY[] = new double[numIons];
         double tempZ[] = new double[numIons];
         int k=0;
         for (int j=1; j<numIons; j++){
              for (int i=0; i<j; i++) {
                   k++;
              }
         }     
         double distance[] = new double[k];
         double tempDistance[] = new double[k];
         double Q[] = new double[numIons];
         for (int i=0; i<numIons; i++){
              Q[i] = Math.pow(-1, (i+2));
         }
         
         //Creates the initial random set of (x,y,z) coordinates
         for (int i=0; i != numIons; i++) {
              arrayX[i] = randomCoord(side);
              arrayY[i] = randomCoord(side);
              arrayZ[i] = randomCoord(side);
              System.out.println("Coordinates for ion "+(i+1)+" are: ");
              System.out.println("("+ arrayX[i]+ ","+ arrayY[i]+","+ arrayZ[i]+")");
         }
         
         //check the distances between the ions
         System.out.println();
         System.out.println("Distances are: ");
         checkDistance(arrayX, arrayY, arrayZ, distance, numIons);
         int l=0;
         for (int i=0; i<k; i++){
              System.out.println(distance[l]+" is the distance "+ l);
              l++;
         }
         System.out.println();
         
         //check the energy between the ions in their initial position
         System.out.println(checkEnergy(distance, numIons, Q)+" is in Joules!");
         System.out.println();
         
         //ask the User how many Trials
         System.out.println("How many Trials?");
         int numTrials = StdIn.readInt();
         System.out.println();

         //Create a new random set of (x,y,z) coordinates by taking a random Step between -1.88 and 1.88 Angstroms
         for (int i=0; i<numIons; i++) {
              double tinyStepX = ((double)(Math.random()*(3.76))- 1.88); //adds a random step between -1.88 and 1.88 Angstroms
              double tinyStepY = ((double)(Math.random()*(3.76))- 1.88);
              double tinyStepZ = ((double)(Math.random()*(3.76))- 1.88);
              tempX[i] = arrayX[i] + tinyStepX;
              tempY[i] = arrayY[i] + tinyStepY;
              tempZ[i] = arrayZ[i] + tinyStepZ;
              System.out.println("Coordinates for ion "+(i+1)+" are: ");
              System.out.println("("+ tempX[i]+ ","+ tempY[i]+","+ tempZ[i]+")");
         }
         
         //check the distances between the ions in the new positions
         System.out.println();
         System.out.println("Distances are: ");
         checkTempDistance(tempX, tempY, tempZ, tempDistance, numIons);
         l=0;
         for (int i=0; i<k; i++){
              System.out.println(tempDistance[l]+" is the distance "+ l);
              l++;
         }
         System.out.println();
         
         //check the energy of the randomly moved ions
         System.out.println(checkTempEnergy(tempDistance, numIons, Q)+" is in Joules!");
         System.out.println();
         
         if ((checkTempEnergy(tempDistance, numIons, Q)) < (checkEnergy(distance, numIons, Q))) {
              for (int i=0; i < numIons; i++) {
                   arrayX[i] = tempX[i];
                   arrayY[i] = tempY[i];
                   arrayZ[i] = tempZ[i];
                   distance[i] = tempDistance[i];
              }
         }
         
         System.out.println(checkEnergy(distance, numIons, Q)+" is in Joules!");
         System.out.println();
    }
    }
    2. Once again, if you report an error you should indicate clearly the line that raises it. Once again you have failed to do this.
    Ok, my bad. The error is in line 200.
    3.. One day you will realize the best way to get help is to write a SSCCE: [http://mindprod.com/jgloss/sscce.html]
    My outputs are supposed to be random, so every output is different. But here is an example: [http://i285.photobucket.com/albums/ll52/RommelTJ/FormingSalt2_help.jpg|http://i285.photobucket.com/albums/ll52/RommelTJ/FormingSalt2_help.jpg] I am really sorry if I offended you or annoyed you in any way. I am just trying to get help and be nice. :( Edited by: RommelTJ on Jun 18, 2008 2:38 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 5. Re: How to overwrite an array?
    DrClap Expert
    Currently Being Moderated
    Ok, my bad. The error is in line 200.
    I gave up trying to count the lines as I scrolled down through your text. The chances that I could find line 200 accurately is basically zero. Which line is line 200?
  • 6. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    DrClap wrote:
    I gave up trying to count the lines as I scrolled down through your text. The chances that I could find line 200 accurately is basically zero. Which line is line 200?
    Line 200 is towards the end where it says:
    distance[i] = tempDistance;
    
    But I figured out my mistake guys! Sorry if I offended Dr. Laszlo. Thanks for your help anyways.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 7. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    RommelTJ wrote:
    Sorry if I offended Dr. Laszlo.
    I'll bet an apology is not necessary. Instead why don't you resolve to change your behavior and follow his recommendations?
  • 8. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    Encephalopathic wrote:
    RommelTJ wrote:
    Sorry if I offended Dr. Laszlo.
    I'll bet an apology is not necessary. Instead why don't you resolve to change your behavior and follow his recommendations?
    Anything over 200 lines can't be a SSCCE *sigh&#42;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  • 9. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    Well, then I do not understand. SSCCE says that it should be short, self-contained, compilable, and that I should post the output. You basically told me that my snippet was unreliable, because I made noobish mistakes. Whatever I typed would be wrong unless it was the complete program, right?

    If you really want me to learn, why don't you show me? I've read the SSCCE page twice, and I fulfilled it to the best of my knowledge!

    My complete program is now posted. How would you have posted my question?

    Please tell me so I can learn to be a better poster.
  • 10. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    RommelTJ wrote:
    Well, then I do not understand. SSCCE says that it should be short, self-contained, compilable, and that I should post the output. You basically told me that my snippet was unreliable, because I made noobish mistakes. Whatever I typed would be wrong unless it was the complete program, right?

    If you really want me to learn, why don't you show me? I've read the SSCCE page twice, and I fulfilled it to the best of my knowledge!

    My complete program is now posted. How would you have posted my question?

    Please tell me so I can learn to be a better poster.
    Reducing your problem, I get the following.
    public class Example {
        public static void main(String[] args) {
            int numIons = 2; //for example
    
            int k=0;
            for (int j=1; j<numIons; j++){
                for (int i=0; i<j; i++) {
                    k++;
                }
            }
    
            //k is now 1
            double distance[] = new double[k];
            double tempDistance[] = new double[k];
    
            for (int i=0; i < numIons; i++) {
                distance[i] = tempDistance;
    }
    }
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 11. Re: How to overwrite an array?
    807601 Newbie
    Currently Being Moderated
    Oh! I see what you mean now. I got it. Thanks!