Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在为分配遍历LinkedList时理解OOP_Java_Oop_Linked List - Fatal编程技术网

Java 在为分配遍历LinkedList时理解OOP

Java 在为分配遍历LinkedList时理解OOP,java,oop,linked-list,Java,Oop,Linked List,我仍然不明白什么时候以及为什么我应该使用“Obj Obj=new Obj”,我也不明白为什么这对我来说如此困难。 在本作业中,我需要创建一个方法,如下所示: “insertSorted:此方法假定输入LinkedList已按非降序排序(即,使每个元素大于或等于其前面的元素,并将输入int值插入列表的正确位置。请注意,该方法不返回任何内容,而是作为副作用修改输入LinkedList。如果输入LinkedList为null,则该方法应终止。这是代码y你从以下几点开始: publicstaticvoi

我仍然不明白什么时候以及为什么我应该使用“Obj Obj=new Obj”,我也不明白为什么这对我来说如此困难。 在本作业中,我需要创建一个方法,如下所示:

“insertSorted:此方法假定输入LinkedList已按非降序排序(即,使每个元素大于或等于其前面的元素,并将输入int值插入列表的正确位置。请注意,该方法不返回任何内容,而是作为副作用修改输入LinkedList。如果输入LinkedList为null,则该方法应终止。这是代码y你从以下几点开始:

publicstaticvoidinsertsorted(LinkedList列表,int值){
/*实施这个方法*/
}
更不用说迭代LinkedList列表的复杂问题了,我甚至不知道如何开始。 我是否应该创建一个新的
LinkedList newList=new LinkedList();
,这样我就可以对它进行迭代了?为什么?如果列表是在方法签名中给出的,我是否应该假设在方法签名中给出输入时,对象已经创建了? 我真的很困惑。似乎我不能完全理解对象编程的全部内容

Obj obj = new Obj
如果你想在一行中理解新的关键字,它就像一个内存中的契约,你可以在其中存储数据(这还不够,但足以开始)

publicstaticvoidinsertsorted(LinkedList列表,int值){
/*实施这个方法*/
}
现在,对于这个方法,您不想创建任何新对象

问:为什么

Ans-当调用此方法时,必须向该方法传递一些参数,否则将是编译时错误


传递的值可以为null。

因为该方法返回void,所以您应该修改作为输入提供的列表。 我将用一个例子来说明原因

public static void insertSorted(LinkedList<Integer> list, int value) {

    LinkedList<Integer> list2 = new LinkedList<Integer>(list); //this means you are creating a new list of integers called list 2 with the same elements of the list "list" in the same order
    //some code to add value to list2 maintaing the sorting
    //no return needed

}
publicstaticvoidinsertsorted(LinkedList列表,int值){
LinkedList list2=新建LinkedList(list);//这意味着您正在创建一个名为list 2的新整数列表,该列表中的元素与列表“list”中的元素顺序相同
//一些代码为列表2添加值,以维护排序
//不需要退货
}
您想在其他地方调用此方法

    LinkedList<Integer> list = new LinkedList<Integer>();

    list.Add(1);
    list.Add(2);
    list.Add(5);

    for (Integer i : list) System.out.println(i);
    //prints 1, 2 , 5
    insertSorted(list,4);
    for (Integer i : list) System.out.println(i);
    //still prints 1, 2 , 5!
LinkedList=新建LinkedList();
增加第(1)款;
增加(2);
增加(5);
对于(整数i:list)System.out.println(i);
//打印1,2,5
插入排序(列表,4);
对于(整数i:list)System.out.println(i);
//仍然打印1,2,5!
如果现在使用调试器运行此代码,并在将值插入list2后立即插入insertSorted方法,您将看到列表“list”保持与方法开头的状态(即(1,2,5)),列表“list2”将是(1,2,4,5)

但是调用方方法对list2一无所知

当你在方法中声明一个变量时,它会在方法结束时消失(除非你返回它)。当然你应该注意“别名”,但事实并非如此

您的要求非常明确:您的方法必须修改输入,而不是创建新列表

您无法理解为什么不必创建新对象就应该创建新对象。有人给了您一个错误的建议:)


现在轮到您迭代列表并在正确的位置插入整数:)

您已经有了一个要迭代的链表。迭代列表应该是直接进行的。您需要为提供的
值创建一个新节点,并迭代直到它大于当前节点的值(并决定如果相等该怎么做),然后使用普通链表语义插入"是..升序。可能有用知道一点你的背景,你只解释了你不知道或不理解的东西,请告诉我你知道的。@Frighi高中毕业后我又开始编码了,忘了它。我正在学习关于计算机科学的ad edX课程,而且,在我看来,整个OOP在p上是清楚的当作业时间到了,我不明白为什么我要做一些事情和其他事情。我想我对编程完全是新手again@DaveNewton就像Node newNode=newNode(value);?编译器说该节点没有在任何地方定义。
LinkedList
Integer
类型,您只需要一个
Integer
public static void insertSorted(LinkedList<Integer> list, int value) {

    LinkedList<Integer> list2 = new LinkedList<Integer>(list); //this means you are creating a new list of integers called list 2 with the same elements of the list "list" in the same order
    //some code to add value to list2 maintaing the sorting
    //no return needed

}
    LinkedList<Integer> list = new LinkedList<Integer>();

    list.Add(1);
    list.Add(2);
    list.Add(5);

    for (Integer i : list) System.out.println(i);
    //prints 1, 2 , 5
    insertSorted(list,4);
    for (Integer i : list) System.out.println(i);
    //still prints 1, 2 , 5!