Hi, I was wondering if any one can help me with returning a array element to the main function. The array list will be the main function. I want to send the array list to a method that adds a element to that list and then send it back to the main function so another method can then receive the array list and print it out. My main problem is how to declare it in the method and how to work it within the method, and returning the array list.

Thanks in advance. GuyOD
btw: I am working with threads.

public class Worker
{

	public static void main(String[] args)
	{
//how do i declare a arraylist here?
		Thread process3 = new Alist(//how do i  pass the array to this method?);
		process3.start();
	}
}

class Alist extends Thread
{
//how do declare it here ?
    public void run()
    {
 //how do i work with just that single element here? 
    }

//how do i return this element of the arraylist to the main function?
}
VernonDozier commented: Used code tags on second post. +21

The way you do it with any other method:

public double method(int i) {
  return i*2.5;
}
....
public static void main(String [] args) {
   double r = method(2);
}

Look the java API (java.util.ArrayList) for the methods and constructor of the ArrayList.

Create a constructor at the Alist class that takes as argument an ArrayList. Hopefully you know how to global attributes in classes, that take value at the constructor and can be used throughout the class.

Declare a boolean in the thread Alist. When the run() starts set its value to false. The last command of the run() would be setting it to true. This variable will tell you if the run() method and Thread has finished or not.

Write a get method in the AList that returns theArrayList.

In the main write a while loop that checks if the Thread is finished or not by checking the boolean variable of the Alist.

If it is finished call the get method of the Alist

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.