Results 1 to 6 of 6

Thread: Incorrect return of ArrayList

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Incorrect return of ArrayList

    Hello!
    I'm manipulating ArrayList <monObjet>. I want to create an ArrayList "molecule" containing amino acids. Each amino acid is an ArrayList of atoms. My problem, c "is that when I do a display at the end of treatment, the ArrayList" molecule ", contains only copies of a AcideAmine the last set. I think my program is returning a incorrect arraylist
    Here's the code:
    Code:
    while (Onln! = null) {
      if (condition){
        / / New amino acid
        AcideAmine cur = new AcideAmine(row.substring(17,20).toCharArray());
        while (autreCondition){ 
          / / Creating an object Atom	 
          atnm = ln.substring(13,16).toCharArray();
          Atom atomTrouve = new Atom(atnm);
          / / Add the atom in the current AA
          cur.addAtom(atomTrouve);
          ln = br.readLine();
        } 
        System.out.print("CurrentAA =");
        cur.print();
        arls.add(cur);
        cur = null ;  / / In case
      }
    }
    }

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Incorrect return of ArrayList

    Hello,
    In your code you have provided, you can create. AcideAmine one with an ArrayList of atoms, right? So if you want you see something wrong because maybe he would have given the rest, Pointers in java . In java pointers exist but you can not handle them. Basically, we talk but no pointer reference object. I hope you are getting my point what I am trying to explain you.

  3. #3
    Join Date
    Dec 2009
    Posts
    296

    Re: Incorrect return of ArrayList

    Hello,
    In fact, what I do is I read a file organized as follows, On each line, there is an atom, and a number corresponding to the amino acid to which it belongs.
    Every time I go in the first while,
    Code:
     AcideAmine cur = new AcideAmine("name")
    
    or AcideAmine is an ArrayList with methods defined
    As is the same amino acid, add to currentAA atoms that I read,
    with the method of ArrayList: add (); Now, when I found a different amino acid, add amino acid to an ArrayList aaArrayList by the method add () and I return to the first while. So logically, in my aaArrayList, I have an ArrayList with all the amino acids that I read, right?

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: Incorrect return of ArrayList

    Hello,
    Just have a look at the following code
    Code:
    AcideAmine cur = new AcideAmine(row.substring(17,20).toCharArray());
        while (autreCondition){ 
          / / Creating an object Atom	 
          atomname = ln.substring(13,16).toCharArray();
          Atom attrv = new Atom(atomname);
          / / Add the atom in the current AA
          cur.addAtom(attrv);
          ln = br.readLine();
        }
    you do not forget to make a line br.readLine = (); before entering the loop?

  5. #5
    Join Date
    Nov 2009
    Posts
    333

    Re: Incorrect return of ArrayList

    Hello
    You create many new AA at every turn, there is no doubt. To be sure he is not the same object, you can make a display of hashCode () in the print method of AcidAmine (if hashCode is not redefined in class AcidAmine). Otherwise, I hope that the attributes of class AcidAmine are not static (indeed it is not possible what I just said).

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Incorrect return of ArrayList

    Hello,
    Code:
    Public static char[] nm;
    Public static ArrayList <Atom> atls;
    Public static ArrayList <int[]> bnls;
    Putting static variables that are the same for all instances of the class (hence the name variable class).
    Replaces by
    Code:
    private static char[] nm;
    private static ArrayList <Atom> atls;
    private static ArrayList <int[]> bnls;
    The private I do not know if it will pass to compile because I have not all your code. Take some reading on the OOP.

Similar Threads

  1. Using an ArrayList
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 12:56 PM
  2. Multithreading and ArrayList
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 05:16 AM
  3. Initialize an ArrayList
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 04:13 AM
  4. ArrayList not working
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 04:18 AM
  5. ArrayList and type the return
    By manjava in forum Software Development
    Replies: 2
    Last Post: 09-05-2009, 11:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,833,557.07863 seconds with 16 queries