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 8897535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:22:11+00:00 2026-06-15T00:22:11+00:00

i have an issue with the understanding the arraylist which contain the objects from

  • 0

i have an issue with the understanding the arraylist which contain the objects from different classes. I have 6 objects. All of objects have 2 attributes in common.(ID,Type)
Every object has its own attributes. I created mainObject class with 2 atr (ID,Type). The other objects extends the mainObject so they have

class mainClass{
    this.id=id;
    this.type=type;
}
class extendedClass extends mainClass{
    super(ID,Type);
    this.atr1=atr1;
}

class extendedClass2 extends mainClass{
    super(ID,type);
    this.atr2=atr2;
    this.atr3=atr3;
}

I read the informations from file.

FileInputStream fstream = new FileInputStream("data.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
    // Print the content on the console
    String s[] = strLine.split("\\|");
    mainClass myObj = new mainClass(s[0], s[1]);

ArrayList<mainClass> items = new ArrayList<mainClass>();
items.add(myObj);

I need all objects to be readed from file line by line and store them in the array list.
How should i do this? I tried the ArrayList<Object> list = new ArrayList<Object>, but its not working. The point is read all the objects from file, sort them due to chosen attribute(id,type).

  • 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-15T00:22:13+00:00Added an answer on June 15, 2026 at 12:22 am

    You are correct that you need a list of mainClass:

    ArrayList<mainClass> items = new ArrayList<mainClass>();
    

    However you should put this line before the while loop, not inside it.

    ArrayList<mainClass> items = new ArrayList<mainClass>();
    
    while ((strLine = br.readLine()) != null) {
        // etc...
        items.add(myObj);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue of alphabetically sorting the contacts picked from the address book
I have an issue which I could not find answer for across the web.
I have an issue while getting the file name with out extension from a
Currently, I have a website which serves dynamic (PHP-MySQL) content from an Apache server,
Like every other web developer on the planet, I have an issue with users
I'm having some trouble understanding this issue. I have a class: class StringProperty {
I have run into an issue that is probably due to my mis-understanding of
I am having trouble understanding this issue - I have a sharded cluster in
i'm having problems understanding how to work with Rack::Test, the issue i have is
Probably a common issue, but I'm looking for help in both fixing and understanding

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.