Reputation: 1
Hi I am a novice n just learning java. I was studying ArrayList n came accross this code for example {CODE1}. I would like to use the same code but add a ArrayListDemo constructor n create methods such as displayList and removeElement. I tried to find such examples but i did not understand them.
This is the code that i tried {CODE2} With my modifications please tell me where m going wrong.
***CODE1 {Example Code}****
import java.util.ArrayList;
public class AraryListDemo {
public static void main(String[] args) {
ArrayList al = new ArrayList();
System.out.print("Initial size of al : " + al.size());
System.out.print("\n");
//add.elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1,"A2");//inserts objects "A2" into array at index 1
System.out.print("size of al after additions " + al.size());
System.out.print("\n");
//display the array list
System.out.print("contents of al: " + al );
System.out.print("\n");
//Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.print("size of after deletions : " + al.size());
System.out.print("\n");
System.out.print("contents of al:" + al);
}
}
********CODE 2 {My Modifications}*************
class ArrayListDemo
{
ArrayList<String> al;//variable declared
ArrayListDemo() throws IOException//try constructor for this
{
al = new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n Enter Student Names");
for(int i=0;i<=5;i++)// will dispaly
{
al.add(br.readLine());
}
}
void dispList(ArrayList <String> al)
{
System.out.println("\n Display Student Names");
for(String str : al)
{
System.out.println("\t Name : "+str+"\n");
}
}
}
class DisplayArrayList
{
public static void main(String []args) throws IOException
{
ArrayList <String> al = new ArrayList <String>();
ArrayListDemo e = new ArrayListDemo();
e.dispList(al);
}
}
Upvotes: 0
Views: 3498
Reputation: 3481
Your code runs as following :-
ArrayList <String> al = new ArrayList <String>(); // Initialise an ArrayList of type string
ArrayListDemo e = new ArrayListDemo(); // Initialised class ArrayListDemo
class constructor reads data from user input and add to ArrayList
a1
by br.readLine()
e.dispList(al);
iterates the ArrayList instance a1
and print its output.
Upvotes: 0
Reputation: 23885
The easiest (but not a prefered) solution to make your effort work is to pass the array to the displist()
method that was filled by the constructor.
public static void main(String []args) throws IOException
{
ArrayListDemo e = new ArrayListDemo();
e.dispList(e.al);
}
Upvotes: 0
Reputation: 77187
It's not clear what exactly you're asking, but I note that you have a problem with your declarations (plural) of al
: You have one ArrayList
named al
in your main
, and you have another one that belongs to ArrayListDemo
. You're reading values into the second one and then printing out the (empty) first one.
You really don't need a separate class with a constructor here. You can just have two static
methods readList(List<String> al)
and dispList(List<String> al)
. If you really do want to have a separate class, pick one place to store the List
(either in main
or in the class).
As a note, it's generally a good idea to use the most general type for variables and method parameters that you can. You're declaring an ArrayList
, which is fine, but if you make your variable and parameters List
s, your code is more flexible.
Upvotes: 0
Reputation: 17622
ArrayList <String> al = new ArrayList <String>();
ArrayListDemo e = new ArrayListDemo();
e.dispList(al);
In the above code, you are creating a new ArrayList
al
, and passing the same to dispList()
method, which doesn't iterate, because the al
has no elements.
I guess you wanted to iterate through the elements which you created within ArrayListDemo
. So you may want to write dispList()
method as below, which will now use ArrayList
defined within the class
void dispList() //method parameter "al" is removed now and, al is the al of ArrayListDemo
{
System.out.println("\n Display Student Names");
for(String str : al) //here al refers to ArrayList defined within the class
{
System.out.println("\t Name : "+str+"\n");
}
}
Upvotes: 2