Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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 如何实现此功能?_Java - Fatal编程技术网

Java 如何实现此功能?

Java 如何实现此功能?,java,Java,我是一个初学者,我想在eclipse中编写Java代码。此程序获取两个LinkedLists整数(例如,a和b),并生成一个LinkedList(例如d),其中每个元素都是a和b中的元素之和。但是,我不能从a和b添加这两个元素,因为它们是对象 例如: a=[3,4,6,7,8] b=[4,3,7,5,3,2,1] ------ d=[7,7,13,12,11,2,1] 我假设您将LinkedList定义为: LinkedList a, b; 在这种情况下,a.get()将返回一个对象。Jav

我是一个初学者,我想在eclipse中编写Java代码。此程序获取两个
LinkedList
s整数(例如,
a
b
),并生成一个
LinkedList
(例如
d
),其中每个元素都是
a
b
中的元素之和。但是,我不能从
a
b
添加这两个元素,因为它们是对象

例如:

a=[3,4,6,7,8]
b=[4,3,7,5,3,2,1]
------
d=[7,7,13,12,11,2,1]

我假设您将LinkedList定义为:

LinkedList a, b;
在这种情况下,
a.get()
将返回一个
对象
。Java支持,允许您执行以下操作:

LinkedList<Integer> a, b;

您需要使用泛型来告诉编译器列表中有整数

List<Integer> a = new LinkedList<Integer>();
List a=newlinkedlist();

如果您无法更改正在使用的列表类型,请在谷歌上搜索java typecast


如果您确实可以控制列表的类型,请像其他人提到的那样使用泛型。

这是家庭作业吗?如果是这样,我不会给你现成的代码,但会给你指出正确的方向

要在Java中创建列表,可以执行以下操作:

List<Integer> list = new ArrayList<Integer>();
Integer five=5; // nicer shorter and easy to understand
Integer six=6; // with auto-boxing
int _six=six; // with auto-unboxing much nicer

把这些放在一起,再加上另一个列表,你就有了答案……

我猜这是家庭作业。我会给你一些代码,但我希望你能从中学习,而不是复制和粘贴。。。人们似乎认为这是错误的,但在我的书中,如果你不够聪明,不能从现成的代码中学习,而只是简单地复制和粘贴,那么最终它将表明你什么也没学到

当我刚开始的时候,我喜欢在我自己尝试之后得到我所面临问题的答案。我学到了很多,希望你也能

正如其他人所说,解决方案基本上是将您的列表声明为通用整数列表:

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


/**
 * @author Savvas Dalkitsis
 */
public class Test {

    public static void main(String[] args) {
        List<Integer> one = Arrays.asList(new Integer[] {3,4,6,7,8});
        List<Integer> two = Arrays.asList(new Integer[] {4,3,7,5,3,2,1});
        List<Integer> add = addLists(one, two);
        printList(one);
        printList(two);
        printList(add);
    }

    public static List<Integer> addLists(List<Integer> a, List<Integer> b) {
        List<Integer> r = new LinkedList<Integer>();
        Iterator<Integer> it = a.iterator();
        for (Integer i: b) {
            r.add(i+(it.hasNext()?it.next():0));
        }
        while (it.hasNext()) {
            r.add(it.next());
        }
        return r;
    }

    public static void printList(List<?> list) {
        for (Object o : list) {
            System.out.print(o.toString()+" , ");
        }
        System.out.println();
    }

}
导入java.util.array;
导入java.util.Iterator;
导入java.util.LinkedList;
导入java.util.List;
/**
*@作者Savvas Dalkitsis
*/
公开课考试{
公共静态void main(字符串[]args){
List one=Arrays.asList(新整数[]{3,4,6,7,8});
List two=Arrays.asList(新整数[]{4,3,7,5,3,2,1});
列表添加=添加列表(一个,两个);
印刷品清单(一份);
印刷品清单(两份);
打印列表(添加);
}
公共静态列表addList(列表a、列表b){
List r=新的LinkedList();
迭代器it=a.Iterator();
for(整数i:b){
r、 添加(i+(it.hasNext()?it.next():0));
}
while(it.hasNext()){
r、 添加(it.next());
}
返回r;
}
公共静态无效打印列表(列表){
用于(对象o:列表){
System.out.print(o.toString()+“,”);
}
System.out.println();
}
}

我想你的问题是:

如果您试图计算的对象的引用属于类Object,那么是的,您有问题,需要将它们转换为整数:

Object o1=object1FromTheList;
Integer integer1=(Integer)o1;

Object o2=object2FromTheList;
Integer integer2=(Integer)o2;
现在,如果您的问题是从它们计算值,如(您得到编译错误):

这意味着您的编译器的版本早于1.5,不支持自动装箱和自动取消装箱:

自动装箱允许简单的代码实现转换:

整数->原语int

Long->primitiveLong

就这样吧

亮点:

如果没有自动装箱,您必须编写:

Integer five=new Integer(5); // very annoying to write this as it is isn't it
Integer six=new Integer(6); // no auto-boxing
int _six=six.getValue(); // no auto-unboxing if you have to do some computation this could be hell
自动装箱允许您这样编写:

List<Integer> list = new ArrayList<Integer>();
Integer five=5; // nicer shorter and easy to understand
Integer six=6; // with auto-boxing
int _six=six; // with auto-unboxing much nicer
如果没有自动取消装箱,您需要编写:

Integer five=new Integer(5); // very annoying to write this as it is isn't it
Integer six=new Integer(6); // no auto-boxing
int _six=six.getValue(); // no auto-unboxing if you have to do some computation this could be hell
自动取消装箱允许您这样编写:

List<Integer> list = new ArrayList<Integer>();
Integer five=5; // nicer shorter and easy to understand
Integer six=6; // with auto-boxing
int _six=six; // with auto-unboxing much nicer
希望这有帮助


Adam。

发布您目前拥有的代码,以便我们能够更好地指出错误所在。您如何尝试从列表中添加值?一些代码会很好。你知道吗?你是独一无二的!千方百计地感谢你,而投票被否决的人认为我不应该提供答案?您可以发布投票被否决的原因,您知道……当您编写类似于
List myList=new LinkedList()的内容时,这是如何调用的
列表myList=new ArrayList()
Map myMap=newhashmap()