How can I implement this feature?

I am new and I want to write Java code in eclipse. This program takes two LinkedListintegers (for example, aand b) and creates LinkedList(for example, d), in which each element is the sum of the elements from aand b. However, I cannot add these two elements from aand b, because they are objects

Example:

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

You need to use generics to tell the compiler that there are integers in the list

List<Integer> a = new LinkedList<Integer>();
+4
source

I assume that you define LinkedLists as:

LinkedList a, b;

a.get() Object. Java generics, :

LinkedList<Integer> a, b;

a.get() Integer Object. , LinkedList , javadoc "Class LinkedList<E>" - E - . get() - public E get(int index) - E, , LinkedList, , .

, add() public boolean add(E o), .

// This is fine
LinkedList a;
a.add(1);
a.add("foo");

// This will fail
LinkedList<Integer> a;
a.add("foo");
+12

, Google java typecast.

, , .

+1

? , , .

Java, - :

List<Integer> list = new ArrayList<Integer>();

, list.add(Integer)

List :

Integer sum = new Integer(0);
for(Integer i : list)
{
    sum+=i;
}

, ...

+1

, . , , , ... , , , , , , , .

, , , . , , .

, , :

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();
    }

}
+1

, :

, , Object, , , Integer:

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

Object o2=object2FromTheList;
Integer integer2=(Integer)o2;

, , ( ):

int sum=integer1+integer2;

, , 1.5, :

- :

Integer -> primitive int

Long → Primitive Long

...

..

so there it is!

highlights:

without automatic boxing you have to write:

Integer five=new Integer(5); // very annoying to write this as it is isn't it

auto-boxing allows you to write like this:

Integer five=5; // nicer shorter and easy to understand

without automatic unpacking you will need to write:

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

auto-unboxing allows you to write it as follows:

Integer six=6; // with auto-boxing
int _six=six; // with auto-unboxing much nicer

hope this helps

Adam.

+1
source

Source: https://habr.com/ru/post/1747074/


All Articles