Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
3/17/2014 8:20:40 AM EDT
so I  have a class project. this is probably the 2nd actual program I had to code.
basically you have to add business and personal contacts to a list.
you have to show all first and last names entered and let them select one and show all the info stored on that person.

my original intent was to create 2 array lists. one with just first and last names. one with all the info.
I realized that I had no way to tie the two lists easlily, and thought I would just create 1 list and pull the first and last names out.

when I had 2 lists, I originally created a thing that would basically string all the info I got together and make it "one entry" for the array.
it basically added the first to the last name, put in a line break, then address, line, the email etc..

I saw an example that let them put in first name, last name etc.. seperately but designate it as say business contact 1 first name, last name etc..
but I don't know if it is business contact 1. it could be business contact 10 since I don't know how many people were entered before.

I can't figure out how to assign a variable to that new contact name so it increments each time.

for example. the way I did it before was just the regular add
people.add (new business(business.getContact()));
this added the whole string as one line.

but if I want to add it like this.
List<Student> Students = new ArrayList();


       Student student1 = new Student();

       student1.setStudentName("Bob Marley");
       student1.setStudentNo("N0002");
       student1.setEmail("[email protected]");
       student1.setYear(2);

how do I get a new student1 to change each time I add a new one?
I know I can figure out how many are in the array first.
I tried it. but it errors out when I try to use a variable in the name.
I also tried to sub in a varriable with the name and that didn't work either.
3/17/2014 10:57:44 AM EDT
[#1]
After filling in all the data into student1, you just need to add it to the list:

student1.setYear(2);
Students.Add(student1);

Each time you call "Student student1 = new Student();" you will get a new student object.
3/17/2014 12:18:20 PM EDT
[#2]
won't they all be student1?

won't the array look like
student1.firstname="abc"



and each time I call that add, it will overwrite that student1.firstname?


I realized, I might try a 2d array and just input each line with all the fields. looking if that works like an arraylist where I can just keep adding to it after I declare the 0,6 type part of the list.
3/17/2014 12:43:00 PM EDT
[#3]
Each time you call List.Add() the data is added to a new node at the end of the list.

Each time you call "student1 = new Student()" the old data (copy now in the list) is discarded and replaced with a new empty Student.

The List is your x-dimensional array.  You just don't have to declare its max size at the start.  Add() appends a new "row" to the end of the List and copies the data to it.
3/17/2014 12:45:12 PM EDT
[#4]
so what I am trying to do is add a new student each time.
but I don't know how many will be added.
how do I change that 1 to something that increments each time?

so each time I call add it is
add student(x)= new student.

because it tosses an error for that and similar things I tried.
3/17/2014 12:49:42 PM EDT
[#5]
Quote History
Quoted:
so what I am trying to do is add a new student each time.
but I don't know how many will be added.
how do I change that 1 to something that increments each time?

so each time I call add it is
add student(x)= new student.

because it tosses an error for that and similar things I tried.
View Quote


See my edited previous post.

You don't need to increment anything manually.  List.Add() does that for you automatically.  Student1 is just a temporary box to put data in.  List.Add() puts a copy of that data into the list (at the end).  The natural thing to do with the contents of Student1 is to discard it at that point.

List<Student> Students = new ArrayList();
Student student1 = null;

for (int x = 1; x <= 10; x++)
{
student1 = new Student();
student1.setStudentName("Bob Marley");
student1.setStudentNo("N0002");
student1.setEmail("[email protected]");
student1.setYear(2);

List.Add(student1);
}

Now that I think about this, I should go look up exactly what an "ArrayList" is....
3/17/2014 12:51:05 PM EDT
[#6]
ah, I see. thanks.
I thought I was having to designate.
that makes it a bit easier.