简体   繁体   English

我可以将ArrayList中的Java值分配给不同的变量而无需对大小进行硬编码吗?

[英]Can I assign Java values from an ArrayList to different variables without hardcoding the size?

I have an ArrayList, and we'll say it can be at most size 5. I want to assign the first element to var1, the second to var2, the third to var3, etc. 我有一个ArrayList,我们会说它最多可以是5号。我想将第一个元素分配给var1,第二个元素分配给var2,第三个元素分配给var3等。

However, sometimes the ArrayList will have less than 5 items, in which case I won't be assigning some variables a value. 但是,有时ArrayList的项目少于5个,在这种情况下,我不会为某些变量赋值。

So my question is, is there a better way to do this other than: 所以我的问题是,有没有比这更好的方法来做到这一点:

if (myArrayList.size() > 0)
    var1 = myArrayList.get(0);
if (myArrayList.size() > 1)
    var2 = myArrayList.get(1);
if (myArrayList.size() > 2)
    var3 = myArrayList.get(2);
if (myArrayList.size() > 3)
    var4 = myArrayList.get(3);
if (myArrayList.size() > 4)
    var5 = myArrayList.get(4);

The source of this is most certainly a bad code design. 这个来源肯定是一个糟糕的代码设计。 Also to what do you initialize the variable if the arraylist doesn't contain the field (after all you use it later)? 如果arraylist不包含该字段(稍后你使用它),你还要初始化变量是什么? A larger example or what exactly you're trying to do would help here. 一个更大的例子或者你正在尝试做什么在这里会有所帮助。 Usually just using 通常只是使用

But I can think of at least two ways to do this: 但我至少可以想到两种方法:

    switch(myArrayList.size()) {
    case 5:
        var4 = myArrayList.get(4);
    case 4:
        var3 = myArrayList.get(3);
    case 3:
        var2 = myArrayList.get(2);
            // and so on
    }

or just use a try/catch. 或者只是使用try / catch。

    try {
        var0 = myArrayList.get(0);
        var1 = myArrayList.get(1);
    }
    catch(IndexOutOfBoundsException ex){
    }

But most certainly it's better to use the arraylist itself and just padd it with the default values you'd otherwise use for your variables. 但是大多数情况下,最好使用arraylist本身,并使用您用于变量的默认值来填充它。

The easiest way to do this is: 最简单的方法是:

Object[] vars = myArrayList.toArray(new Object[5]);

If you insist on having the variables var1 through var5 rather than just using the array elements, copy the array elements to the variables. 如果您坚持使用变量var1到var5而不是仅使用数组元素,请将数组元素复制到变量中。 Alternatively you can replace all instances in your code of "= varN" with "=myArrayList.get(n)". 或者,您可以将“= varN”代码中的所有实例替换为“= myArrayList.get(n)”。

How about using a separate array and assigning the value for each array for corresponding value on the list in a loop? 如何使用单独的数组并为循环中的列表上的相应值分配每个数组的值?

for (i = 0; i<= 5; i++) {
var [i] = myArrayList.indexOf(i);
}

I agree with Voo that this is most likely from bad code design. 我同意Voo的观点,这很可能来自糟糕的代码设计。 But it's an opportunity for me to demonstrate my love of final variables and ternary expressions. 但这是我展示我对最终变量和三元表达式的热爱的机会。 :-) How's this? :-) 这个怎么样? (For yuks I'm presuming an ArrayList of Strings.) (对于yuks,我假设是一个字符串的ArrayList。)

final Iterator<String> iter = myArrayList.iterator();
final String var1 = iter.hasNext() ? iter.next() : null;
final String var2 = iter.hasNext() ? iter.next() : null;
final String var3 = iter.hasNext() ? iter.next() : null;
final String var4 = iter.hasNext() ? iter.next() : null;
final String var5 = iter.hasNext() ? iter.next() : null;

The described design look realy ugly. 所描述的设计看起来很丑陋。 You can use ternary operator for desired purpose though: 您可以使用三元运算符达到预期目的:

var1 = myArrayList.size() > 0 ? myArrayList.get(0) : "null";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中定义没有硬编码的数组大小 - defining an array size without hardcoding in Java java - 如何在没有硬编码的情况下将类名分配给Java中的注释的属性 - How to assign the class name to the property of an annotation in java without hardcoding 如何从 Java 中的不同 class 访问 arraylist? - How can I access an an arraylist from a different class in Java? 用Java硬编码值 - Hardcoding values in Java 我可以在Java中创建不同类型的ArrayList - Can I create an ArrayList of different types in Java 在Java中,当我处于线程扩展类的run()函数中时,无法为变量赋值 - in Java, I can't assign values to variables while i'm in the run() function of a thread-extended class UriComponentsBuilder 从现有 URI 构造 URI,无需对 URI 变量进行硬编码 - UriComponentsBuilder construct a URI from existing URI without hardcoding URI variables 如何在不硬编码 IP 的情况下欺骗 Java RMI/JMX 从外部本地主机访问? - How to I trick Java RMI/JMX to be accessible from outside localhost without hardcoding the IP? 如何为 arraylist 中的变量分配名称? - How do I assign names to variables in an arraylist? 如何从 Java 中的 2 个不同类访问变量 - How can I access variables from 2 different classes in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM