可以在Java中创建一个不需要为其创建类的不同元素列表。

huangapple go评论75阅读模式
英文:

can you make a list of differents element in java without making a class for it

问题

我需要创建一个列表,每次迭代都包含一个整数(int)、日期和一个注释(字符串)。

所以我需要的是这样的内容:
List A = {[5, 12/12/2020, "note1"], [6, 11/01/2021, "note2"], ...}
我需要的是对此的声明。我知道我可以将其更改为list<list>,然后将所有东西从字符串更改回我想要的类型,或者声明一个包含该信息的类,并创建一个该类的列表,但我的问题是是否有一种方法可以在不这样做的情况下实现(不转换为字符串,也不声明新类)。
感谢您提供的任何帮助。

英文:

i need a create a list that contains in each iteration an int ,date and a note (String)

so what i need is something like this :
List A ={[5,12/12/2020, "note1"],[6,11/01/2021,"note2"],...}
what i need is the declaration for this . and i knew that i can change it to a list<list< String >> then change everything back from String to the type of thing that i want or declare a class that contains that information and make a list of that class but what i'm asking is there a way to make without doing that (without changing to String and without declaring a new class) .
thank you for any help you offer

答案1

得分: 2

这是你在寻找的吗?

List<Object[]> lista = new ArrayList<Object[]>();

lista.add(new Object[] {5, LocalDate.of(2020, 12, 12), "note1"});
lista.add(new Object[] {6, LocalDate.of(2021, 1, 11), "note2"});
英文:

Is that what you are looking for?

List&lt;Object[]&gt; lista = new ArrayList&lt;Object[]&gt;();

	lista.add(new Object[] {5, LocalDate.of(12,12,2020),  &quot;note1&quot;});
	lista.add(new Object[] {6, LocalDate.of(11,1,2021),  &quot;note2&quot;});

答案2

得分: 0

我认为你想要的可能是不可能的,至少不是以你想要的方式精确实现。

根据Java SE API,List 是“一个有序集合”,而 Collection 则表示“一组对象,称为其元素”。详见 List 和/或 Collection

List 的每个“元素”只能是一个单独的对象,所以拥有一个实际上是“3”个单独实体的元素与其实现相悖。

但正如你所指出的,对象元素可以包含任何你想要的内容。但正如你所说,在你的情况下这不是一个可行的选项。

我确信,对于更了解Java的人可能会提出一些方法来实现你想要的,但我有一种感觉,此时你可能会创建一个绕过List核心功能的hack方法,在这种情况下,使用List可能不是最佳途径。

听起来你在描述一个List,其中每个元素包含一个int、一个Date和一个String。因此,创建一个包含这三个属性的类,然后将其用作List的每个对象元素,将会正确地利用List的实现,而不是试图使其执行其不打算执行的操作。

但我很好奇听听其他人的意见,希望他们能够提供比我更多的帮助!祝你好运!

英文:

I don't think what you want is possible, at least not exactly the way you want it.

According to the the Java SE API, a List is "An ordered collection" and a Collection "represents a group of objects, known as its elements". See List
and/or Collection.

Each "element" of a List can only be a single object, so having an element that's actually "3" separate entities goes against its implementation.

But as you pointed out, that object element can contain whatever you want. But as you said, that's not a viable option in your case unfortunately.

I'm sure someone with more knowledge of Java can possibly suggest some method to achieve what you want, but I have a feeling at that point you'd be creating a hack to circumvent some of the core functionality of theList implementation, in which case using a List may not be the best route.

It sounds like you're describing a List where each element contains an int, a Date, and a String. So, creating a class that contains those 3 properties that can then be used as each object element of the List would be utilizing the List's implementation correctly as opposed to trying to make it do something it's not intended to do.

But I'm curious to see what others say and hope they can provide more help than I'm capable of providing! Good luck!

答案3

得分: 0

你可以告诉集合只存储对象,所以基本上你可以把任何想要的东西放进去,然后以后再取出来。这是不推荐的(TM)。

import java.util.*;

public class Rando
{
    public static void main(String args[])
    {
        List<Object> randomStuff = new ArrayList<>();

        randomStuff.add(1);
        randomStuff.add(new Date());
        randomStuff.add("A note");

        System.out.println(randomStuff);

        Map<String, Object> randomMap = new HashMap<>();

        randomMap.put("id", 1);
        randomMap.put("date", new Date());
        randomMap.put("note", "This is another note");

        System.out.println(randomMap);
    }
}
英文:

You can tell Collections to just store Objects, so you can stuff pretty much anything you want in there then pull it out later. This is Not Recommended(tm).

import java.util.*;

public class Rando
{
    public static void main(String args[])
    {
        List&lt;Object&gt; randomStuff = new ArrayList&lt;&gt;();

        randomStuff.add(1);
        randomStuff.add(new Date());
        randomStuff.add(&quot;A note&quot;);

        System.out.println(randomStuff);

        Map&lt;String, Object&gt; randomMap = new HashMap&lt;&gt;();

        randomMap.put(&quot;id&quot;, 1);
        randomMap.put(&quot;date&quot;, new Date());
        randomMap.put(&quot;note&quot;, &quot;This is another note&quot;);

        System.out.println(randomMap);
    }
}

huangapple
  • 本文由 发表于 2020年9月9日 08:02:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63803014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定