0

I did search on internet, but still don't understand, so I ask this question here.

in the small program below, I created two tour instance, all I want to do is putting tour[2] in without changing "Tour tour[]=new Tour[2];".

A lot of people recommend ArrayList, but I don't know how to do it in this code.

class Test{
public static void main(String args[]){

    class Tour{
        private String tourId;
        private String tourDescription;
        private double tourFee;
        private int numOfBooking;

        public Tour(String tourId,String tourDescription,double tourFee){
            this.tourId=tourId;
            this.tourDescription=tourDescription;
            this.tourFee=tourFee;
        }

        public void print(){
            System.out.println("ID:"+this.tourId);
            System.out.println("Desc:"+this.tourDescription);
            System.out.println("Fee:"+this.tourFee);
        }
    }


    Tour tour[]=new Tour[2];
    tour[0]=new Tour("AB001","TOUR1",100);
    tour[1]=new Tour("AB002","TOUR2",200);
}
}

8 Answers 8

1

As you discovered, arrays are indeed fixed length, and as such I would definitely recommend an ArrayList. Use it like this...

ArrayList<Tour> tours = new ArrayList<Tour>();
tours.add(new Tour("AB001","TOUR1",100));
tours.add(new Tour("AB002","TOUR2",200));

Every time you want to add a new Tour, just call tours.add() again.

Sign up to request clarification or add additional context in comments.

3 Comments

the problem is, the actual code is longer than this, and have a different functions, if I change the code like you said, I think I need to change most of the program?
You don't have much of a choice. You can't change the length of an array.
Yeah its a bit annoying, but once you know how to do it, you'll remember it from now on, and you'll never create the same problem again :-) If you've got a lot of them, try using find/replace in your editor so that you minimise the number of changes.
1

When you create an array in java like Tour tour[] = new Tour[2] this means you can only add 2 elements to it.

To implement this using an ArrayList you would:

In you import statements add

import java.util.ArrayList

Then replace your array code with this

ArrayList<Tour> tour = new ArrayList<Tour>();
tour.add(new Tour("AB001", "TOUR1", 100);
tour.add(new Tour("AB002", "TOUR2", 200);
tour.add(new Tour("AB003", "TOUR3", 300);

Comments

0

If you're trying to add a tour[2], then your array needs three elements, right?

Otherwise:

List<Tour> tour = new ArrayList<Tour>();
tour.add(new Tour("AB001", "TOUR1", 100);
tour.add(new Tour("AB002", "TOUR2", 200);
tour.add(new Tour("AB003", "TOUR3", 300);

Comments

0

As your array scenario is impossible, here's how to do the same process with an array list:

ArrayList<Tour> tours = new ArrayList<Tour>();
tours.add(new Tour("AB001","TOUR1",100));
tours.add(new Tour("AB002","TOUR2",200));
tours.add(/*another tour, and so forth*/);
// to convert to a Tour[], use the following:
Tour[] tourArray = tours.toArray(new Tour[tours.size()]);

ArrayList's are extremely useful in cases where the size of the data set is unknown.

Comments

0

using array list is simple, you have this code :

Tour tour[]=new Tour[2];
tour[0]=new Tour("AB001","TOUR1",100);
tour[1]=new Tour("AB002","TOUR2",200);

replace it with this :

ArrayList<Tour> tours = new ArrayList<Tour>();
tours.add(new Tour("AB001","TOUR1",100));
tours.add(new Tour("AB002","TOUR2",200));

to get elements arrayList , simply use tour.get( index ) ;

Comments

0

They are right about the ArrayList; use it like this:

List<Tour> tours = new ArrayList<Tour>();
tours.add(new Tour("AB001","TOUR1",100));
tours.add(new Tour("AB002","TOUR2",200));

you will need to import java.util.*

Comments

0

This is not possible unless assigned new. Because primitive arrays are not expandable.
Alternatively you can use collections to dynamically add a new object to the list.
An example is shown below:

List<Tour> tourList = new ArrayList<Tour>( 10 );  
tourList.add( 0, new Tour( "AB001", "TOUR1", 100 ) ); // add at first    
tourList.add( 1, new Tour( "AB002", "TOUR2", 200 ) ); // add next to first   

This way you can add any number of Tour instances at any position of the list.
And you may want to convert the tourList back to a primitive array of Tour
For that you need following code:

Tour [] toursArray = new Tour[ tourList.size() ];
tourList.toArray( toursArray );

Comments

0

You cannot do it in with arrays, since their length are fixed.
Your question pretty much shows the alternatives already:

  1. Use an ArrayList
  2. Create a new instacne of the array

The ArrayList is actually an implementation of a dynamic array in java, and this is most likely what you actually want.

Instantiate it with a default constructor, and add elements with ArrayList.add(index,element) to add elements in specific location or with ArrayList.add(element) to append elements.

code snap:

List<Tour> tours = new ArrayList<Tour>();
tours.add(new Tour("AB001","TOUR1",100));
tours.add(new Tour("AB001","TOUR1",100));

You can keep appending elements using ArrayList.add(element)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.