You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin中如何向列表添加泛型类型实例?需在init方法添加指定数量空对象

Hey there! Let me help you work through this problem—adding a specific number of empty objects to a list right in your __init__ method is a common need for setting up initial class state, so let’s break this down clearly.

First, let’s clarify what "empty object" usually means here: either a placeholder instance of a custom class (with default or uninitialized properties) or a generic placeholder like None. I’ll cover both scenarios, plus tie in the type variance concepts you mentioned.

Solution for Adding N Empty Objects to a List in __init__

1. Adding distinct instances of a custom empty class

If you’re looking to create multiple separate instances of a custom "empty" object (the most common use case), use a list comprehension to avoid a common pitfall of referencing the same object repeatedly. Here’s a Python example (adjustable to other typed languages like C#/Java too):

# Define your "empty" object class—can have default properties or be a simple placeholder
class EmptyItem:
    pass

class MyClass:
    def __init__(self, num_empty_objects: int):
        # Create a list with N distinct EmptyItem instances
        self.item_list = [EmptyItem() for _ in range(num_empty_objects)]

# Usage: Creates a list with 7 empty EmptyItem instances
my_instance = MyClass(7)
print(len(my_instance.item_list))  # Output: 7

Critical note:

Avoid using [EmptyItem()] * num_empty_objects—this creates a list where all elements point to the same object, which is almost never what you want. The list comprehension ensures each entry is a unique instance.

2. Using None as placeholder values

If you just need temporary empty placeholders to populate later, this simpler approach works:

class MyClass:
    def __init__(self, num_placeholders: int):
        self.item_list = [None] * num_placeholders

# Usage: List with 4 None placeholders
my_instance = MyClass(4)
print(my_instance.item_list)  # Output: [None, None, None, None]

Aligning with type variance concepts

Since you mentioned researching variant/invariant/covariant types, here’s how to ensure type safety (using Python’s type hints as an example):

from typing import List

class EmptyItem:
    pass

# If you need subclass compatibility (covariance), you can use List[EmptyItem]
# which accepts subclasses of EmptyItem too
class MyClass:
    def __init__(self, num_objects: int):
        self.item_list: List[EmptyItem] = [EmptyItem() for _ in range(num_objects)]

This ensures your list’s type is consistent with the objects you’re adding, avoiding variance-related type errors you might have encountered earlier.

If you’re working in a specific language (like C#, Java, or TypeScript), let me know and I can tweak the example to fit that context!

内容的提问来源于stack exchange,提问作者Johan Vergeer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:02:56