Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:48:10+00:00 2026-06-17T20:48:10+00:00

I am newly learning java.I am trying to understand the various data structures in

  • 0

I am newly learning java.I am trying to understand the various data structures in util package.

/*create an ArrayList object*/
List<String> arrayList = new ArrayList<String>();

In the decleration of ArrayList class I have seen

public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable

My question is if I use an object of type List<> to store the object returned by constructor of ArrayList<>

will it work.You may assume that I need to use only the methods specified in the List interface,such as add() or size().

I have tried this on my machine and it does not work.I found this in the java tutorial below

http://www.javamex.com/tutorials/collections/using_1.shtml

The complete example code that I tried is as below.

package arraylist;
import java.util.ArrayList;
import java.util.List;

public class ArrayListExample {

        public static void main(String[] args)
        {

        /*create an ArrayList object*/
         List<String> arrayList = new ArrayList<String>();

                /*
                Add elements to Arraylist using
                boolean add(Object o) method. It returns true as a general behavior
                of Collection.add method. The specified object is appended at the end
                of the ArrayList.
                 */
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");

        /*
        Use get method of Java ArrayList class to display elements of ArrayList.
        Object get(int index) returns and element at the specified index in
        the ArrayList
         */
        System.out.println("Getting elements of ArrayList");
        System.out.println(arrayList.get(0));
        System.out.println(arrayList.get(1));
        System.out.println(arrayList.get(2));
    }
}

I forgot to import List.that is why it did not work.However my next question is we use a variable of type List to store on object of type ArrayList.

Comparing the methods of List and ArrayList ArrayList has a method called trimToSize() that is not there in list.

In the above case will I be able to call that method?

If yes,what that generally means is that a base class pointer can be used to store a derived class object as the method list of derived class will always be a super-set of base class methods?

Is my above conclusion correct? It my sound stupid question but I am very new to java.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:48:12+00:00Added an answer on June 17, 2026 at 8:48 pm

    Since ArrayList implements the List interface you should be able to use the object declared as a List that is initialized to an ArrayList. This is pretty common practice. It allows you to switch the implementation of the list without code changes.

    Make sure your importing the list interface:

    package arraylist;
    import java.util.ArrayList;
    import java.util.List;
    
    public class ArrayListExample {
    
            public static void main(String[] args)
            {
    
            /*create an ArrayList object*/
             List<String> arrayList = new ArrayList<String>();
    
                    /*
                    Add elements to Arraylist using
                    boolean add(Object o) method. It returns true as a general behavior
                    of Collection.add method. The specified object is appended at the end
                    of the ArrayList.
                     */
            arrayList.add("1");
            arrayList.add("2");
            arrayList.add("3");
    
            /*
            Use get method of Java ArrayList class to display elements of ArrayList.
            Object get(int index) returns and element at the specified index in
            the ArrayList
             */
            System.out.println("Getting elements of ArrayList");
            System.out.println(arrayList.get(0));
            System.out.println(arrayList.get(1));
            System.out.println(arrayList.get(2));
        }
    }
    

    When referencing the List you will not be able to use the trimToSize function since it is not on the interface for List

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning java newly. Can anybody clear my doubt in following? My demo application has
I am barely learning MySQL (moving from SQLite). I am trying to create a
I'm learning C and i'm trying to do the following: create a structure table
I am newly started using WAMP server. I am unable to deploy my java
How to find data usage of newly installed application? getUidTxBytes(int uid) and getUidRxBytes(int uid)
Is there a way to pass a newly created object(not saved yet) to another
I am trying to make a very simple window manager for learning purposes. I
am not sure if I'm doing this right, but I'm trying to create a
I'm trying to make an Applescript that takes a list of names and makes
Symptoms I have newly created setup package (MSI) that installs a new version of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.