6 Replies - 376 Views - Last Post: 15 April 2012 - 05:11 PM Rate Topic: -----

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Dynamically adding objects to Arraylist of objects

Posted 15 April 2012 - 03:23 PM

I am trying to make an arraylist that holds objects but what i am trying to do is create objects to put in the arraylist but change the name of the object each time it's called. So like this:


//Create objects of Student class

Student s1 = new Student("Alex", 80);

I am trying to change the s1 part of the code so it would be like every time it runs it it would change to s2, s3, s4, etc.

This is the code i am trying to use to make it happen

id = (accounts1.size() + 1);
            Random rand = new Random();
            int randBalance = rand.nextInt(10000);
            String accId = ("CA" + id);
            
            Account accId = new Account(id, "Savings", randBalance);
            accounts1.add(id, accId, randBalance);

This post has been edited by robgw3: 15 April 2012 - 03:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically adding objects to Arraylist of objects

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Dynamically adding objects to Arraylist of objects

Posted 15 April 2012 - 03:31 PM

use an array
it will be al.add(s[0]), al.add(s[1]), ...
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,839
  • Joined: 05-April 11

Re: Dynamically adding objects to Arraylist of objects

Posted 15 April 2012 - 03:32 PM

Why would you wanna do that? Explain why you would want to do it, and maybe one of us know a better solution
Was This Post Helpful? 0
  • +
  • -

#4 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Dynamically adding objects to Arraylist of objects

Posted 15 April 2012 - 03:47 PM

Well because i want to be able to call and use the values from each object like s1.name s2.name, etc without needing to know what index it is in in the array.

Trying to think of a better way to explain it.
Was This Post Helpful? 0
  • +
  • -

#5 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Dynamically adding objects to Arraylist of objects

Posted 15 April 2012 - 04:02 PM

Even if you don't need (or want) to know the indices of where the element is located, it might be a better idea to traverse the ArrayList and get the corresponding object.

For example, suppose I want a student with the name "Alex", I could do (in pseudocode):

getStudent(String studentName)
for each (Student tempStudent in ArrayList students
   if (tempStudent.getName().equals(studentName)
       return tempStudent;

return null; //means that the student was not found



The object this method would return, will still allow you to use all the methods and variables related to that student.
Even if this method only returns the first encountered "Alex" in my list, you can easily extend it. I could use a new ArrayList to store all the occurrences of "Alex" in my current ArrayList.

Just an idea ;)

Although, I am curious why you'd want to use "your" method over this arraylist traversal method.

This post has been edited by karabasf: 15 April 2012 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Dynamically adding objects to Arraylist of objects

Posted 15 April 2012 - 04:43 PM

Actually got a little mixed up there, this is the code i am using right now, was mixed up thinking about something else i had been trying.

class Accounts {
        
        int accountId;
        String accountType;
        int balance;
        int id;
            
        public Accounts(int accountId, String accountType, int balance) {
            this.accountId = accountId;
            this.accountType = accountType;
            this.balance = balance;
            
        }
    }


And

class SavingsListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            ArrayList<Accounts> accounts1 = new ArrayList<Accounts>();

            id = (accounts1.size() + 1);
            Random rand = new Random();
            int randBalance = rand.nextInt(10000);
            type = "Savings";
            accounts1.add(id, type, randBalance);
            
        }
        
    } 


The last line there accounts1.add is giving me an error and i cant figure out why.
It's saying "No suitable method for add(int, java.lang.string, int)" and something about actual and formal arguements differ in length.
Was This Post Helpful? 0
  • +
  • -

#7 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: Dynamically adding objects to Arraylist of objects

Posted 15 April 2012 - 05:11 PM

Never mind, found the problem.
Was This Post Helpful? -1
  • +
  • -

Page 1 of 1