ArrayList and Generics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • redashley
    New Member
    • Apr 2007
    • 8

    #1

    ArrayList and Generics

    Hello Everyone.
    I have some question to ask you I have read your info and have read a book call Big Java and I still lost.
    1. I have a Class called BankStatement and when I'm create a Arraylist.
    Arraylist <BankStatemen t> bank = New Arraylist <BankStatemen t>
    is <BankStatemen t> the Class BankStatement.
    2. How do I inunate BankStatement can can if be null, and how do I do this and where do I do this.
    3. How do I add data to bank and where do I add it at.
    I think it
    Code:
    BankStatement bank = new BankStatement("A1") //is A1 the Index number/
    bank.addBid("123Aw");
    bank.addFname("Red");
    ...
    arlist.add(bank);
    4. can you have more than I arraylist in a Class. ie project.

    thanks
    Red
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by redashley
    Hello Everyone.
    I have some question to ask you I have read your info and have read a book call Big Java and I still lost.
    1. I have a Class called BankStatement and when I'm create a Arraylist.
    Arraylist <BankStatemen t> bank = New Arraylist <BankStatemen t>
    is <BankStatemen t> the Class BankStatement.
    2. How do I inunate BankStatement can can if be null, and how do I do this and where do I do this.
    3. How do I add data to bank and where do I add it at.
    I think it
    Code:
    BankStatement bank = new BankStatement("A1") //is A1 the Index number/
    bank.addBid("123Aw");
    bank.addFname("Red");
    ...
    arlist.add(bank);
    4. can you have more than I arraylist in a Class. ie project.

    thanks
    Red
    Changed thread title.

    Not all that you have asked is very clear. bank is a BankStatement object, so to set any data in it you need to use the methods that you have defined in the BankStatement class.
    The arraylist declaration is actually supposed to be

    Code:
     
    ArrayList<BankStatement> list = new ArrayList<BankStatement>();

    Comment

    • redashley
      New Member
      • Apr 2007
      • 8

      #3
      So if I have a class BankStatement

      Code:
      class BankStatement{
      String BankId;
      String Fname;
      String Lname;
      }
      	/*
      	 * Mutator Methods if any
      	 */
      	/*
      	 * accessors make info available if any.
      	 */
      //create my arraylist.
      public class BankStatement{
      ArrayList<BankStatement> list = new ArrayList<BankStatement>();
      }
      Is that right?

      Red.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by redashley
        So if I have a class BankStatement

        Code:
        class BankStatement{
        String BankId;
        String Fname;
        String Lname;
        }
        	/*
        	 * Mutator Methods if any
        	 */
        	/*
        	 * accessors make info available if any.
        	 */
        //create my arraylist.
        public class BankStatement{
        ArrayList<BankStatement> list = new ArrayList<BankStatement>();
        }
        Is that right?

        Red.
        You have two BankStatement classes there! Also remember in java, all code must be inside some class. If you get time go through this.

        Comment

        • redashley
          New Member
          • Apr 2007
          • 8

          #5
          Opps...
          Code:
          class BankInfo{
          String BankId;
          String Fname;
          String Lname;
          }
          	/*
          	 * Mutator Methods if any
          	 */
          	/*
          	 * accessors make info available if any.
          	 */
          //create my arraylist.
          public class BankStatement{
          ArrayList<BankInfo> list = new ArrayList<BankInfo>();
          }
          So to add object into the Arraylist I would do somthing like this.
          BankInfo temp = new Bankinfo("B1)//this being the index postion.
          temp.addBankId( "BC-123");
          temp.addFname(" Red");
          temp.addLname(" Ashley");
          ....
          arlist.add(temp ;

          is that right.
          if so where do I place this info at.
          Red

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by redashley
            Opps...
            Code:
            class BankInfo{
            String BankId;
            String Fname;
            String Lname;
            }
            	/*
            	 * Mutator Methods if any
            	 */
            	/*
            	 * accessors make info available if any.
            	 */
            //create my arraylist.
            public class BankStatement{
            ArrayList<BankInfo> list = new ArrayList<BankInfo>();
            }
            So to add object into the Arraylist I would do somthing like this.
            BankInfo temp = new Bankinfo("B1)//this being the index postion.
            temp.addBankId( "BC-123");
            temp.addFname(" Red");
            temp.addLname(" Ashley");
            ....
            arlist.add(temp ;

            is that right.
            if so where do I place this info at.
            Red
            I don't understand what you mean by

            "//this being the index postion"

            but to add to the ArrayList you do
            Code:
             arlist.add(temp);
            where arlist is the ArrayList of BankInfo and temp is a BankInfo object.

            Comment

            • redashley
              New Member
              • Apr 2007
              • 8

              #7
              Originally posted by r035198x
              I don't understand what you mean by

              "//this being the index postion"

              but to add to the ArrayList you do
              Code:
               arlist.add(temp);
              where arlist is the ArrayList of BankInfo and temp is a BankInfo object.
              this being the index postion I mean this is where the object will be stored in the Arraylist.

              red

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by redashley
                this being the index postion I mean this is where the object will be stored in the Arraylist.

                red
                You are mistaken then. The add method is the one that adds to the arraylist. Have a look at the docs for ArrayList.

                Comment

                • redashley
                  New Member
                  • Apr 2007
                  • 8

                  #9
                  Thanks r035198x for the help. I know there well be more Question for you.

                  Comment

                  Working...