View Full Version : Array list questions
fobber
01-21-2007, 12:48 AM
Hi all,
I would like to know how to write an Array list containing strings and store it into a class.
Also, I would like to know how to read an arraylist from this example:
public static Account readAccount(String fileName)
{
Account aAcct = new Account(0, fileName);
ArrayList ArrayList = new ArrayList();
aAcct = new Account("123", "Al Bob", "213 123 4567");
return aAcct;
}
Aradon
01-21-2007, 07:51 AM
Hi all,
I would like to know how to write an Array list containing strings and store it into a class.
Also, I would like to know how to read an arraylist from this example:
public static Account readAccount(String fileName)
{
Account aAcct = new Account(0, fileName);
ArrayList ArrayList = new ArrayList();
aAcct = new Account("123", "Al Bob", "213 123 4567");
return aAcct;
}
You could go through the array list just like you would go through an array using a for loop.
Arraylist has methods such as size and get(int) that allow you to do this. So for example
import java.util.*;
public class testArrayList
{
public static void main(String args[])
{
ArrayList<String> lst = new ArrayList<String>();
lst.add("this");
lst.add("is");
lst.add("an");
lst.add("ArrayList");
for(int i = 0; i < lst.size(); i++)
System.out.print(lst.get(i) + " ");
}
}
You can also do it through Iterators and through foreach loops!
java foreach:
import java.util.*;
public class testArrayList
{
public static void main(String args[])
{
ArrayList<String> lst = new ArrayList<String>();
lst.add("this");
lst.add("is");
lst.add("an");
lst.add("ArrayList");
for(String element ; lst)
System.out.print(element + " ");
}
}
fobber
01-21-2007, 09:09 PM
Thank you for replying.
I guess for my question regarding array list, is I want any user to put certain strings, and then store it into an array.
An example of what I am looking for is the bank account, which I want to store the account number, name, and a balance (double). I am looking to create different accounts, and store it to an array.
From my GUI, it looks like this
String inputName;
String inputPhoneNum;
inputName = JOptionPane.showInputDialog("Enter your name: ");
inputPhoneNum = JOptionPane.showInputDialog("Enter your phone number: ");
String input = JOptionPane.showInputDialog("Enter the deposit amount: ");
double amount = Double.parseDouble(input);
Teller tel = new Teller();
tel.createAccount(inputName, inputPhoneNum, amount);
I want to put 2 strings and 1 double in an array. I would like to create a new account and store it. The problem is I dont know how to write that part.
public void writeAccount(ArrayList aAccts, String name, String accNum,
String phoneNum, double amount)
{
/*this. aAccts = aAccts;
this.name = name;
this.accNum = accNum;
this.phoneNum = phoneNum;
this.amount = amount;*/
ArrayList aL = new ArrayList();
aL.add(name);
aL.add(accNum);
aL.add(phoneNum);
//aL.addAll(amount);
System.out.print(aAccts);
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.