在 java.util 中获取错误 ArrayList 不带参数 [英] Getting Error ArrayList in java.util does not take parameters

查看:61
本文介绍了在 java.util 中获取错误 ArrayList 不带参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个Student"类,现在我试图将我的 Student 对象存储在一个 ArrayList 中,但是在编译 ArrayList 不接受参数时出现错误.我已经多次检查我的代码,但无法找到问题.

I've made a class "Student" now I'm trying to store my Student objects in an ArrayList but I'm getting error while compiling that ArrayList does not take accepts parameters. I've checked my code many time but unable to find the problem.

我的学生班是这个

public class Student
{
//declaring variables 
private int rollNumber ;
private String name ;


//a counter for counting how many students or objects are declaired yet
public static int studentCount = 0;
public static int getStudentCount()
{
    return studentCount;
}


//getters for both variables
public int getRollNumber()
{
    return rollNumber;
}
public String getName()
{
    return name;
}


//setters for both variables 
public void setRollNumber(int x)
{
    if (x > 0)
    {
        this.rollNumber = x;
    }
    else
    {
        System.out.println("Wrong Roll Number Value");
    }
}
public void setName(String someName)
{
    this.name = someName;
}


//default constructor
public Student()
{
    setName("Please Enter A Name");
    this.rollNumber = 0;
    studentCount +=1;
}


//parameterized constructor
public Student(String name, int x)
{
    setName(name);
    setRollNumber(x);
    studentCount += 1;
}


//copy constructor
public Student(Student s)
{
    setName(s.name);
    setRollNumber(s.rollNumber);
    studentCount +=1;
}

//defining an print method 
public void print()
{
    System.out.println("Student Name = " + name + "______Roll Number = " + rollNumber);
}
}

我在其中使用它的类如下

and the class in which i'm implimenting it is as follows

import java.util.*;

public class ArrayList
{
    public static void main (String[] anyString)
{
    //Creating Some student objects
    Student s1 = new Student("Ammar" , 1);
    Student s2 = new Student("Ahmad" , 2);
    Student s3 = new Student("Arslan", 3);


    //Creating an arraylist
    ArrayList<Student> someList= new ArrayList<Student>();


    //checking if our Array List is empty or not 
    boolean empty = someList.isEmpty();
    if (empty == true)
        System.out.println("ArrayList is Empty \n\n");


    //adding our student class objects to our arraylist
    someList.add(s1);
    someList.add(s2);
    someList.add(s3);


    //checking if arraylist is empty or not 
    empty = someList.isEmpty();
    if (empty == true)
        System.out.println("ArrayList is Empty");
    else
    //counting total members in a list 
    System.out.println("Our ArrayList someList have a total of " +someList.size() + " Members \n\n");


    //geting back objects from arraylist or extractin them from list 
    //we will display our objects to console one by one 
    for (int i = 0; i < someList.size() ; i++)
    {
        Student one = someList.get(i);
        one.print();
    }
}
}

推荐答案

清理并重命名.

import java.util.ArrayList;

public class Student
{
  String name;

  int id;

  public Student(String name, int id)
  {
    this.name = name;
    this.id = id;
  }

  public static void main(String[] anyString)
  {
    // Creating Some student objects
    Student s1 = new Student("Ammar", 1);
    Student s2 = new Student("Ahmad", 2);
    Student s3 = new Student("Arslan", 3);

    // Creating an arraylist
    ArrayList<Student> someList = new ArrayList<Student>();

    // checking if our Array List is empty or not
    boolean empty = someList.isEmpty();
    if (empty == true)
      System.out.println("ArrayList is Empty \n\n");

    // adding our student class objects to our arraylist
    someList.add(s1);
    someList.add(s2);
    someList.add(s3);

    // checking if arraylist is empty or not
    if (someList.isEmpty())
      System.out.println("ArrayList is Empty");
    else
      // counting total members in a list
      System.out.printf("Our ArrayList someList has a total of %d Members \n\n",
          someList.size());

    // geting back objects from arraylist or extracting them from list
    // we will display our objects to console one by one
    for (Student student : someList)
    {
      System.out.println(student);
    }
  }

  @Override
  public String toString()
  {
    return "Student [name=" + name + ", id=" + id + "]";
  }
}

这篇关于在 java.util 中获取错误 ArrayList 不带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆