ArrayList的在Java中,增加code至方法 [英] ArrayList in Java, adding code to methods

查看:117
本文介绍了ArrayList的在Java中,增加code至方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我在我的code在我的课ArrayListTest最终得到一个编译时错误的两个方法的头?

ArrayListTest: http://pastebin.com/dUHn9vPr

学生: http://pastebin.com/3Vz1Aytr

我有两行编译器错误:

 删除(CS242,S3)更换(CS242,S,S4);

当我尝试运行code将其规定:

 异常线程mainjava.lang.Error:未解决的编译问题:
    该方法取代(的ArrayList<学生>中的学生,学生)在类型ArrayListTest不适用于参数(列表<学生>中的学生,学生)
    该方法删除;在类型ArrayListTest(ArrayList的&LT学生>中的学生),不适用的参数(列表<学生>中生)
        在ArrayListTest.main(ArrayListTest.java:54)

我固定的编译时错误,因为我使用Eclipse和Eclipse提供了$ C $选项C,可用于修复编译时错误。我选择更改方法替换(ArrayList中,学生,学生)'到'替代(列表,学生,学生)

虽然固定的编译时错误,我不明白为什么我得到一个编译时错误开始与和为什么有效地固定误差

我真的不知道是什么失踪code我需要写这两种方法如下更正:

 公共静态无效替换(列表<的Student GT; cS242,学生oldItem,
                学生的newitem){公共静态无效删除(列表<的Student GT; cS242,学生目标){


解决方案

这是因为你被宣告你的的ArrayList<学生> 为:

 列表<的Student GT; CS242 =新的ArrayList<的Student GT;(25);

和您的替换方法:

 公共静态无效取代(的ArrayList<的Student GT; ALIST,学生oldItem,学生的newitem){
}

有关Java中,要传递的东西是一个列表来那将只与 ArrayList的工作方法秒。的如果的它允许你这样做,你可以举例来说,改变实现为的LinkedList<学生> ,仍然把它传递给替换这将是不正确的。

或者使用替换(列表<学生>)或声明 CS242 的ArrayList&LT ;学生> CS242 ,尽管前者被认为是最好的做法。

Why am I getting a compile time error for the two method headers at the end of my code in my class ArrayListTest?

ArrayListTest: http://pastebin.com/dUHn9vPr

Student: http://pastebin.com/3Vz1Aytr

I have a compiler error at the two lines:

delete(CS242, s3)

replace(CS242, s, s4);

When I try to run the code it states:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The method replace(ArrayList<Student>, Student, Student)in the type ArrayListTest is not applicable for the arguments (List<Student>, Student, Student)
    The method delete(ArrayList<Student>, Student) in the type ArrayListTest is not applicable for the arguments (List<Student>, Student)
        at ArrayListTest.main(ArrayListTest.java:54)

I fixed the compile time errors since I use Eclipse and Eclipse gives options of code you can use to fix the compile time error. I chose to "Change method to 'replace(ArrayList, Student, Student)' to 'replace(List, Student, Student)'

Although it fixed that compile time error, I don't understand why I was getting a compile time error to begin with and why that effectively fixed the error

I don't really know what missing code I need to write to correct these two methods below:

public static void replace(List<Student> cS242, Student oldItem,
                Student newItem) {

public static void delete(List<Student> cS242, Student target){

解决方案

That's because you were declaring your ArrayList<Student> as:

List<Student> CS242 = new ArrayList<Student>(25);

And your replace method as:

public static void replace(ArrayList<Student> aList, Student oldItem, Student newItem) {
}

For Java, you are passing something that is a List to a method that'll only work with ArrayLists. If it allowed you to do that, you could for example, change the implementation to a LinkedList<Student>, and still pass it to replace which wouldn't be correct.

Either use replace(List<Student>), or declare CS242 as ArrayList<Student> CS242, although the former is considered best practice.

这篇关于ArrayList的在Java中,增加code至方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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