Rafique Abdullah
Rafique Abdullah

Reputation: 13

Can we create ArrayList of a class inside that class in Java?

Salam!

In java, i required to create an ArrayList of a class inside that class. Is It Possible??? As i know that if i create object of a class in that class constructor, then it will cause StackOverFlow. Complete detail of the problem is given below. How can i create ArrayList of BookInfo class, in that class itself???

Problem Statement:
You are required to write a java program which contains only two classes named as “BookInfo” and “BookMgtSys”.

BookInfo class must have the following data members: • ISBN • Book Name • Author Name • Total Books When you will add book then it will increment variable Total Books and decrement when you will delete book.

BookInfo class must have the following member methods: • Default constructor • Parameterized constructor • addbook ()
• searchBook () • deleteBook ()

BookMgtSys is a public driver class that contains the main() method. The name of you file should be BookMgtSys as it is a public class in your program.

Detailed Description:

Default constructor: The default constructor will create an ArrayList.

Parameterized constructor: It should take three String parameters named as (ISBN, Book Name, and Author Name) and initializes the data members values with the passed parameters. addBook(): This method will ask the user to enter ISBN, Book name and Author name and then add the book in ArrayList and increment the Total Book variable. After successful adding the book, A message box should be displayed containing Book information. searchBook(): This method takes one parameter which is book name and search the book name in the ArrayList. If search is found then display the Book information otherwise display message that Book not found. deleteBook(): This method takes one parameter which is book name and delete the book from the ArrayList and then decrement the Total Book variable.

Thanks for your kind help.

Code is given below. But it not compiles. It gives following error:

C:\Code>javac BookMgtSys.java BookMgtSys.java:39: error: -> expected bookList = new ArrayList() (); ^ BookMgtSys.java:39: error: ';' expected bookList = new ArrayList() (); ^ BookMgtSys.java:67: error: reached end of file while parsing } ^ 3 errors

import javax.swing.*;
import java.util.*;
public class BookMgtSys
{
public static void main(String[] args)
{

System.out.println("Bismillah");
BookInfo Books=new BookInfo();
String  UserOption = JOptionPane.showInputDialog("Enter 1 to Add Book\r\nEnter 2 to Search Book\r\nEnter 3 to Delete Book\r\nEnter 4 to Exit System");
switch(UserOption)
{
    case "1":
    //  Books.addbook();
    break;
    case "2":
    break;
    case "3":
    break;
    case "4":
    break;

}
//JOptionPane.showMessageDialog(null, "You entered " + ipstring);
}
}
class BookInfo
{
 String ISBN;
 String BookName;
 String AuthorName;
 static int TotalBooks;
ArrayList<BookInfo> bookList;
//constructor
// ArrayList<BookInfo> bookList;
//BookInfo class must have the following member methods:
public BookInfo()
{
    bookList = new ArrayList() <BookInfo>();
//persons = new ArrayList()<PersonInfo>();
}
// public void BookInfo(String ISBN, String BookName, String AuthorName)
// {
// this.ISBN=ISBN;
// this.BookName=BookName;
// this.AuthorName=AuthorName;
// TotalBooks++;
// }
 // public void addbook ()
 // {
     // // this.ISBN=JOptionPane.showInputDialog("Enter The ISBN");
     // // this.BookName=JOptionPane.showInputDialog("Enter The Name");
     // // this.AuthorName=JOptionPane.showInputDialog("Enter The Author");

      // TotalBooks++;
 // }
// public void searchBook ()
// {

// }
// public void deleteBook ()
// {
    // if(TotalBooks>0)
        // TotalBooks--;
// }

}

Upvotes: 0

Views: 3282

Answers (3)

Fundhor
Fundhor

Reputation: 3577

 ArrayList<BookInfo> bookList = new ArrayList<BookInfo>();

But please see @IhorYatsenko's answer if it solves your problem.

Upvotes: 2

Ihor Yatsenko
Ihor Yatsenko

Reputation: 91

Please post your code here?

You wrote new ArrayList() <BookInfo>();, yes? If true, remove a brackets after ArrayList.

Upvotes: 2

Jim Garrison
Jim Garrison

Reputation: 86764

Creating an ArrayList<T> inside the constructor of class T will not create a recursive loop, because you are not creating additional instances of T, merely a container into which you can later add T instances.

Upvotes: 3

Related Questions