5 Replies - 10985 Views - Last Post: 17 April 2010 - 04:11 AM Rate Topic: -----

#1 Guest_OzzyB*


Reputation:

Calling a arraylist method in another class

Posted 16 April 2010 - 01:14 PM

I am designing a cinema system

I want to call a another class which has a arraylist method and then display the elements inside the arraylist.

Please help

I cannot post the code because its a university project
Is This A Good Question/Topic? 0

Replies To: Calling a arraylist method in another class

#2 Guest_OzzyB*


Reputation:

Re: Calling a arraylist method in another class

Posted 16 April 2010 - 01:28 PM

I made two sample classes

Class 1
Class cinema()
	.
	.
	.
	.

	public ArrayList<time> getTime()
	{
		for (Show show : getTime){
        	}
        	return getTime;
    
	}

}



Class 2
Class System()
.
.
.
.
.
private void welcome()
{

Cinema listOftime = new Cinema();
}

I don't known how to call & display the arraylist.

hope that helps

Thanks
Was This Post Helpful? 0

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: Calling a arraylist method in another class

Posted 16 April 2010 - 04:43 PM

If getTime is the name of your instance variable in the Cinema class, you can invoke the getTime() method from your Cinema object (bad practice to name your variable the same thing as your method) and have an ArrayList<time> object point to it. Like so:
ArrayList<time> list = listOfTimes.getTime();



Also, the class keyword in Java is lower-cased, and classes are not declared with parenthesis. In Java (and most languages), parentheses denote functions or methods.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Ozzie*


Reputation:

Re: Calling a arraylist method in another class

Posted 17 April 2010 - 02:42 AM

thanks but how do you display all the elements in the arraylist?
Was This Post Helpful? 0

#5 darek9576  Icon User is online

  • D.I.C Lover

Reputation: 198
  • Posts: 1,688
  • Joined: 13-March 10

Re: Calling a arraylist method in another class

Posted 17 April 2010 - 03:54 AM

You can use an Iterator in order to display all elements in an arraylist:
but remember that first you need to import the iterator from java.util package.
Therefore:


import java.util.ArrayList;
import java.util.Iterator;

public class SampleClass{
	private ArrayList list;
	

	public void display(){
		Iterator<Object> it = list.iterator();
		
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}
	
}



method display() where you create iterator object ("it") and then use its built in methods
hasNext() checks whether there is another object in an arrayList and next() just gets the object
remember that everything that comes out of the arraylist is of type Object so you might have to cast stuff
Note: you will need to adapt the above code to work with your code

Hope that helps..

darek
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: Calling a arraylist method in another class

Posted 17 April 2010 - 04:11 AM

Don't forget to declare the generic type of your ArrayList using parameterization, so as to avoid deprecation warnings. So if you want to store an ArrayList of Strings, you would write ArrayList<String> as the type.

Also, no need to use an Iterator in this case when we can use a for loop or a foreach loop. It will be more memory efficient. So for example:
for(int i = 0; i < listName.size(); i++){
   //access ArrayList using get(int index) method
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1