Qin
Qin

Reputation: 63

Adding new element into a fixed array

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);
}
}

Upvotes: 0

Views: 2818

Answers (8)

amit
amit

Reputation: 178411

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)

Upvotes: 0

Ravinder Reddy
Ravinder Reddy

Reputation: 23982

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 );

Upvotes: 0

Tom
Tom

Reputation: 4180

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.*

Upvotes: 0

Hayden
Hayden

Reputation: 2808

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);

Upvotes: 1

fahim ayat
fahim ayat

Reputation: 312

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 ) ;

Upvotes: 0

FThompson
FThompson

Reputation: 28687

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.

Upvotes: 0

wattostudios
wattostudios

Reputation: 8764

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.

Upvotes: 1

Jeremy Goodell
Jeremy Goodell

Reputation: 18952

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);

Upvotes: 0

Related Questions