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

  • SEARCH
  • Home
  • 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 8767527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:50:17+00:00 2026-06-13T16:50:17+00:00

I have a class which holds a list inside a list, and its declared

  • 0

I have a class which holds a list inside a list, and its declared as type String, but i no longer know that its a string.

private ArrayList<List<String>> table;
table = new ArrayList<List<String>>();
table.add(new ArrayList<String>()); //is possible
table.add(new ArrayList<Integer>()); //not possible

But now the nested list could either be string or integer. How can i fix that? I also can’t declare Object must either be string or integer

Also can a single Arraylist hold both ints and strings without declaring as objects?

Edit: It seems you guys are debating the right answer. I will insight why i chose Vikdor answer.

My job is to load a csv, and then user types in command to define each column as string or int. My table class has a private instance variable, and has methods which adds the values. Doing it in Vikdor way makes performing operations on table very easy where i want to merge two table or sort. It will also prevent user from inputting non string or int through an exception.

  • 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-13T16:50:18+00:00Added an answer on June 13, 2026 at 4:50 pm

    You might be looking for this:

    List<List<? extends Object>> table;
    table = new ArrayList<List<? extends Object>>();
    table.add(new ArrayList<String>()); 
    table.add(new ArrayList<Integer>()); 
    

    This would result in unchecked code when you retrieve the inner lists, as follows (which obviously is not recommended in production code):

    List<String> stringList = (List<String>) table.get(0); // Unchecked cast.
    

    From OP’s comment:

    Because its my requirement to have it as either int or string. Else you can put more than those types in the list. The nested list holds value of a table in columns and each column holds int or string. Do you have any other dynamic way of reading csv file and storing in table?

    It is better to use a proper CSV processor (like SuperCSV) that would parse directly into a POJO with appropriate data types associated with each column.

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

Sidebar

Related Questions

So I have a class which holds a struct inside its private variables and
I have a class that holds a few vectors, I'm not sure which method
I have a class called GestorePersonale which holds a list of instances of another
I have class which holds a an integer and another list holds the data.
In my android project, I have a class which holds list of stream addresses
Let's say I have a class Collection which holds a list of Items. public
I have a singleton class called Manager that holds a list of object instances:
I have a MapTile class which holds a tile sprite and the tile's attribute.
I have written an NHibernateSessionFactory class which holds a static Nhibernate ISessionFactory. This is
I have a class which is essentially just holds a bunch of constant definitions

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.