Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Data is covered by latter data

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Data is covered by latter data

    Hi everyone
    I want to add some data to an ArrayList array, but the latter data just covers the former one, while the size of the array is growing (I learned Java for 4 months by myself and did not take any data structure courses in my college...)
    here is the code (they are in the same class):
    ArrayList<ArrayList<ArrayList<Double>>> all = new ArrayList<ArrayList<ArrayList<Double>>>();
    ArrayList<ArrayList<Double>> calcOutput(ArrayList<ArrayList<Double>> input) throws Exception {
    ArrayList<ArrayList<Double>> out = new ArrayList<ArrayList<Double>>();
    //calculation;
    return out;
    }
    void allfire(ArrayList<ArrayList<ArrayList<Double>>> tch) throws Exception {
    for (int p = 0; p < tch.size(); p++) {
    all.add(calcOutput(tch.get(p)));
    }
    it happens that for p=1 or 2 or else, data in all is always changed to the last data added into all
    how does this happen and how to solve it?
    Thanks very much


  2. #2
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Data is covered by latter data

    sounds like your are recreating your ArrayList<....<Double> >> somewhere down the line. can you post more of the code? Do you need a three dimensional ArrayList of ArrayList?

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Data is covered by latter data

    Are you sure you need such monsters:
    ArrayList<ArrayList<ArrayList<Double>>> all = new ArrayList<ArrayList<ArrayList<Double>>>(); ?

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Data is covered by latter data

    My codes is boring in that I have no idea about data structure and object-oriented idea and made it a mess
    public class Network {
    ArrayList<ArrayList<ArrayList<Double>>> all = new ArrayList<ArrayList<ArrayList<Double>>>();
    double[] weight;
    ArrayList<ArrayList<Double>> calcOutput(ArrayList<ArrayList<Double>> input) throws Exception {
    ArrayList<ArrayList<Double>> out = new ArrayList<ArrayList<Double>>();
    //calculate with parameter weight[] and add the data into out
    //weight[] will be changed in allfire method
    return out;
    }//end of calcOutput method
    void allfire(ArrayList<ArrayList<ArrayList<Double>>> tch) throws Exception {
    for (int pattern = 0; pattern < tch.size(); pattern++) {
    boolean test = true;
    while(test){// loop until the result(out) is perfect
    // calc out
    ArrayList<ArrayList<Double>> out = new ArrayList<ArrayList<Double>>();
    out = calcOutput(tch.get(pattern));
    //if the result(out) is not perfect, change parameter(weight[]) for a better result(a better out)
    }//end of loop while
    }//end of loop for
    //weight[] is good enough to make perfect out for all patterns
    //calculate out with this weight[] and add the data into all
    all.clear();
    for (int p = 0; p < tch.size(); p++) {
    all.add(calcOutput(tch.get(p)));
    }
    }//end of allfire method
    }//end of class

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Data is covered by latter data

    for example
    for p=0 add [[3.0],[3.2,3.3]] into all
    for p=1 add [[4.2,5.3]] into all
    then data in all turns out to be[[[4.2,5.3]],[[4.2,5.3]]] which should be [[[3.0],[3.2,3.3]],[[4.2,5.3]]]
    seems that for each calcOutput(tch.get(p)),it is the data in the memory changed so that the former data is also changed

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,439
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Data is covered by latter data

    Do you have code to test your program? I don't see a main() method or any calls to your methods.

Similar Threads

  1. Data Structure
    By Faha in forum Object Oriented Programming
    Replies: 9
    Last Post: November 10th, 2011, 12:35 AM
  2. Replies: 2
    Last Post: June 15th, 2011, 03:49 PM
  3. Replies: 1
    Last Post: June 11th, 2011, 05:39 AM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. I can't key in the data
    By muffin in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 10th, 2009, 11:03 PM