Hey.

I have an arraylist;

public  static ArrayList<String> name = new ArrayList<String>();

and its made of of names;

public  static String  name;

They both had to be static as it didn't work if they weren't.

In the public void static main method, I populate this arraylist.

I then want to use this populated arraylist in another method, but when I go to print its contents(passed in as parameter) its empty!

>

Recommended Answers

All 13 Replies

You are probably creating multiple ArrayLists but without seeing the code no one could really say.

import java.util.*;


public class initial {
		
	public char separator = '/';
	public  static String  word;
	public  static ArrayList<String> words = new ArrayList<String>();
	
	public static void main(String args[]){
		{
			
			String sentence = "Glasgow/Celtic/Football/Club";
			StringTokenizer st = new StringTokenizer(sentence,"/");
			while (st.hasMoreTokens()) {
				words.add(st.nextToken());
				
			  }
			Collections.sort(words);
			System.out.println(words);
		
		
		}
	}
	
	public void hele(ArrayList<String> words)
	{
		System.out.println(words.get(0));

You can't print the ArrayList with a single println() call, as you are trying in main().

Your 'hele' method would print the first element in the collection, but you aren't calling that method anywhere anyway.

Ok then. How do I get it to print out other things such as the hele method? I need the main method so I can't get rid of that print.

Thanks.

To call your 'hele' method, it would need to be declared static.

To print the list, loop through the ArrayList and println() each element.

public static void hele()
	{
		System.out.println("sd");
	
	}

This doesn't work. Still the arraylist from the main method is the only thing that prints.

I just done this method to try and get it to work, just to let you know.

Sorry about this.

Methods do not execute by themselves - you have to call them. Did you put a call to "hele()" in your main method?

No, I don't understand.

Usually if I didn't have the main method, I could have just done what I did with the hele method and it would have worked.

No I didn't call it in the main method, as it has no use in there. I need to use the ArrayList I built in the main method, in a separate method and print things out in that method.

You need to review how methods work. To execute the code in a method you must call it, so in main() you would have to add a line

hele();

if you want that code to run. If you want to pass it the array list then you must change your declaration back to take the ArrayList parameter and call it like so

hele(words);

Hmmm ok. I want to take the first element of the array and add it to a tree. I then want to print this tree. Does this need to be done in the main? Or can it all be done in another method(I have defined a tree)

It depends where you defined the tree object. If you defined the tree object within main, then you can only use the tree object within main. The only exception to this rule is if you pass the tree object as a parameter to a method.

http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html

You may also want to research 'variable scope' - which means what I said, that if you declare a variable in main, it can only be used there, just like if you declare a variable in any method, it can only be used there (unless you pass that variable as an argument to another method). In Java, if you declare a variable as a static class variable, you could use it throughout that class in any static (or non-static) method.

I have a seperate class for defining my tree.

My method;
static void name(Tree t)

when called in main doesn't accept the parameter?

I.e name(Tree t);

When you call the method you just pass the Object. So in main,

methodName(yourTreeName);

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.