i am a beginner in java and this is my first project my project is about cinema which have 10 theaters and i need to provide set of films to each theater i try to do this but i am a bit confused and lost

import java.util.Arrays;
import java.util.List;
import java.util.Iterator; 

public class Cinema {  
 public static void main(String[] args) { 
  String[] strArray = new String[]

{"Matrix","Avatar","HarryPotter","Taken","The reader"};     

List list = Arrays.asList(strArray);  
Iterator itr = list.iterator();     
System.out.println("List of movies,");    while(itr.hasNext())      System.out.println(itr.next());   } }

how i can link a seperate set of films to each theater?

The first step is to make sure you fully understand the concept of an arraylist. Once you do, check out this code -- I think the simplest approach is to use an arraylist of arraylists to represent the theaters:

import java.util.ArrayList;
public class Cinema
    public static void main(String[] args)
        //This will embed 2 array lists together -- basically each element in the theatres arraylist will itself be an arraylist of strings, each of which represent a film.
        ArrayList<ArrayList<String>> theaters = new ArrayList<ArrayList<String>>(10);
        //To access a theater, use the syntax: theaters.get(index)
        ArrayList<String> list = theaters.get(0); //returns the first theater's film list.
        //To access a film from ArrayList list, use the syntax: list(index)
        String film1 = list.get(0); //returns the first theater's film list.
        //To do this in one step, use the syntax: theaters.get(theater index).get(film index);
        film1 = theaters.get(0).get(0); //returns the first film playing at the first theater.

        //To print all the films of all the theaters, use a nested for loop:
        for (int i = 0; i < theater.size(); i++)
            System.out.print("Theater "+(i+1)+": ");
            for (int j = 0;j<theater.get(i).size(); j++)
                System.out.print(theater.get(i).get(j)+", ");

Note that all the normal arraylist methods such as add, set, etc. all apply here, even in embedded arraylists.

Edited 6 Years Ago by kvass: n/a

This article has been dead for over six months. Start a new discussion instead.