Hi i have created a list which reads from a text file. how can i view the contennts of the list outside of the method it was created. I have inserted my code below.

//read Customers.txt and save in an object. 
		List<Customer2> list = new ArrayList<Customer2>();
		File file = new File("Customers.txt");
		try {
		BufferedReader in = new BufferedReader(new FileReader(file));
		String line = null;
 
		while ((line = in.readLine()) != null) {
		String[] record = line.split(",");
		Customer2 customer = new Customer2();
		customer.setName(record[0]);
		customer.setAddress(record[1]);
		customer.setCustomerNumber(record[2]);
		customer.setEmailAddress(record[3]);
		customer.setPassword(record[4]);
		list.add(customer);
		}
 
		} 
		//catch exception
		catch (Exception e) {
			e.printStackTrace();
		}
 
 			// tell the user how many objects have been added
			System.out.println("Customers Added: ");
			System.out.print("" + list.size());
			System.out.println("");

for example could i get the size of that list from another class or method other than the one it was defined in? sorry if this is really obvious i am just beginning to use this language.

thanks!

return the list.

public List<Customer2> whatever() {
    List<Customer2> list = new ArrayList<Customer2>();
    ....
    return list;
}

I have tried your code but whenever i call that method i simply get nothing from the list. e.g. list size = 0

thanks

Loop through the list like an array. Only this time use the size() as upper limit. Use the get(int i) method to get the element of the list.

Have you looked at the API for the List? If you are having problems with a class, always look the API:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html


And about masijade's code. You were supposed to fill the little dots with your code. The code that puts elements in the list

If the list has already been created, all you need in the new method is

public List<Customer2> getCustomers() {
    return list;
}

and the easiest and safest way to loop thru a list is

for (Customer2 cust : getCustomers()) {
   System.out.prinltln(cust.getName()); // or whatever
}

Well, I'm not sure if I understand the problem correctly. It's like.. you want the contents of the list outside the method and the size should be the modified one... so... why not have the list global?
Or return the list from the method and call the method where you want to use the list.
how about that?

If you would like to read the list in any other methods you could declare the list as an instance variable instead of a local variable.
Like for example, you have posted a method definition... something like ...

public class X {
public void methodX() {
        List<Customer2> list = new ArrayList<Customer2>();
.....;
.....;
}
methodY() {
int size = list.size(); //This is wrong....methodY cannot see list... 
}
}

If you would like methodY to see your list, you would need to declare it as an instance or a class variable..
something like this...

public class X {
public List<Customer2> list = new ArrayList<Customer2>();
public void methodX() {
.....;
.....;
}
methodY() {
         int size = list.size(); //This works fine
}
}

In this case methodY can see the list because the list is an instance or an object variable.

But its not a good idea to declare an instance variable as public. Hence a better and more elegant way has already been discussed above i.e. to pass the reference to the list using return. This way the variable can be declared as a private instance variable or even a local variable to a method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.