简体   繁体   中英

C# List implementation… in Java

I'm learning Java and I'm trying to do something I've always done in C#... in Java.

The goal is to wrap the class "MyItem" into a List of "MyItems". This makes it easier to reason about ( MyItems is much easier to read/understand than List<MyItem> ... or, say, I need to make it something more complicated like IEnumerable<KeyValuePair<string,List<Dictionary<int,bool>>>> ... then ask for a MyKVPDictionaries instead of List<IEnumberaable<KeyValuePair<...>>>>> ).

Just looking around... It seems like C# allows you to keep the default implementations of stuff (Look below, .Add just works).

Looking at Java... is there a way to implement a list as done in c#? Or do I have to manually implement the individual parts of the List manually? (.add, .list, .contains, etc).

Below I have a "basic" implementation of a Class ... and a List<Class> in C#.

Is it really that much more work to implement class MyItems implements List<MyItem> in Java or am I missing something to simplify the process?

(The Java code is only the MyItems.java class file with Resharper "Auto Implement missing members" stubs via IntelliJ).

C# version .NetFiddle :

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {       
        var myItems = new MyItems();
        myItems.Add(new MyItem("Hello"));
        myItems.Add(new MyItem(" "));
        myItems.Add(new MyItem("World"));
        myItems.Add(new MyItem("!"));

        foreach(var item in myItems)
            Console.Write(item.Name);
    }
}

public class MyItems : List<MyItem>
{

}

public class MyItem
{
    public MyItem(string name)
    {
        Name = name;
    }
    public string Name { get; private set; }
}

Java Version start/stub:

package items;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MyItems implements List<MyItem> {
    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<MyItem> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public boolean add(MyItem generatePreSignedUrl) {
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends MyItem> c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends MyItem> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public MyItem get(int index) {
        return null;
    }

    @Override
    public MyItem set(int index, MyItem element) {
        return null;
    }

    @Override
    public void add(int index, MyItem element) {

    }

    @Override
    public MyItem remove(int index) {
        return null;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<MyItem> listIterator() {
        return null;
    }

    @Override
    public ListIterator<MyItem> listIterator(int index) {
        return null;
    }

    @Override
    public List<MyItem> subList(int fromIndex, int toIndex) {
        return null;
    }
}

Java's List type is an interface, the counterpart of C#'s IList . You'll have to write most of the methods from scratch if you want to implement it. The counterpart of C#'s concrete List class would be Java's ArrayList :

public class MyItems extends ArrayList<MyItem> {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM