[英]ArrayList constructor and use in methods
Hi I am a novice n just learning java. 嗨,我是新手,只是学习java。 I was studying ArrayList n came accross this code for example {CODE1}. 我正在研究ArrayList n来自这个代码,例如{CODE1}。 I would like to use the same code but add a ArrayListDemo constructor n create methods such as displayList and removeElement. 我想使用相同的代码,但添加一个ArrayListDemo构造函数n创建方法,如displayList和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. 这是我试过的代码{CODE2}我的修改请告诉我哪里出错了。
*** CODE1 {Example Code} *** CODE1 {示例代码} ** * * ** * *
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} ** * ** * ** 代码2 {我的修改} ** * ** * ** * **** ** * ** * ** * ****
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);
}
}
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 . 在上面的代码中,您将创建一个新的ArrayList
al
,并将其传递给不迭代的dispList()
方法,因为al
没有元素 。
I guess you wanted to iterate through the elements which you created within ArrayListDemo
. 我想你想迭代你在ArrayListDemo
创建的元素。 So you may want to write dispList()
method as below, which will now use ArrayList
defined within the class 所以你可能想要编写dispList()
方法,它现在将使用类中定义的ArrayList
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");
}
}
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
. 目前还不清楚你问什么,但我注意到,你有一个问题,您的声明(复数) al
:你有一个ArrayList
命名al
在你的main
,你有另外一个属于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)
. 您可以只有两个static
方法readList(List<String> al)
和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). 如果您确实想要一个单独的类,请选择一个位置来存储List
(在main
或类中)。
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. 您正在声明一个ArrayList
,这很好,但是如果您创建变量和参数List
,则代码更灵活。
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. 使您努力工作的最简单(但不是首选)解决方案是将数组传递给由构造函数填充的displist()
方法。
public static void main(String []args) throws IOException
{
ArrayListDemo e = new ArrayListDemo();
e.dispList(e.al);
}
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()
类构造函数从用户输入读取数据并通过br.readLine()
添加到ArrayList
a1
e.dispList(al);
iterates the ArrayList instance a1
and print its output. 迭代ArrayList实例a1
并打印其输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.