简体   繁体   English

无法将元素添加到ArrayList <Integer>

[英]Can not add elements to ArrayList<Integer>

So I am trying to create a ArrayList of Integers and then iterate variables into it. 所以我试图创建一个整数数组列表,然后将变量迭代到其中。 Only the first value that I specify is being added. 仅添加我指定的第一个值。

 private ArrayList<Integer> heights(String detail) {
        ArrayList<Integer> heights = new ArrayList<Integer>();
        heights.add(0);
        switch(detail) {
            case "L": for(Integer i = 100; i <= 1000; i=+50) { heights.add(i); } break;
            case "H": for(Integer i = 100; i <= 1000; i=+25) { heights.add(i); } break;
        }
        return heights;
    }

Value of heights("H"): 高度值(“ H”):

heights: [0] 高度:[0]

The Problem is in for loop. 问题在for循环中。 Use int instead of Integer class.... 使用int代替Integer类。

The code below works properly... 下面的代码正常工作...

import java.util.*;  
class Test{
        private ArrayList<Integer> heights(String detail) {
            ArrayList<Integer> heights = new ArrayList<Integer>();
            heights.add(0);
            switch(detail) {
                case "L": for(int i = 100; i <= 1000; i=i+50) { heights.add(i); } break;
                case "H": for(int i = 100; i <= 1000; i=i+25) { heights.add(i); } break;
            }
            return heights;
        }
     public static void main(String args[]){
            Test t = new Test();
            System.out.println(t.heights("H"));
         }
    } 

暂无
暂无

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

相关问题 如何将元素从一个有序整数ArrayList添加到另一个整数有序ArrayList? - How can I add elements from one ordered integer ArrayList to another integer ordered ArrayList? 如何添加一个ArrayList <Integer> 到ArrayList <ArrayList<Integer> &gt;一次又一次地使用ArrayList中的不同元素 <Integer> - How to add an ArrayList<Integer> to an ArrayList<ArrayList<Integer>> again and again with different elements in ArrayList<Integer> 在ArrayList中添加整数 <ArrayList<Integer> &gt; - Add Integer in ArrayList<ArrayList<Integer>> 如何在Java中将键添加为Integer并且值为arraylist的HashMap中添加元素? - how to add elements to HashMap whose key is Integer and value is arraylist in Java? 无法将更多元素添加到ArrayList - Can't add more elements to the ArrayList Java - ArrayList元素的排列(整数) - 无法使其正常工作 - Java - Permutation of ArrayList elements (Integer) - Can't get it to work properly 如何将 Arraylist 的最后 3 个元素添加到不同的 Arraylist? - How can I add the last 3 elements of an Arraylist to a different Arraylist? 如何在 ArrayList 中交换元素<arraylist<integer> &gt;? </arraylist<integer> - How to swap elements in ArrayList<ArrayList<Integer>>? 反转ArrayList中的元素 <Integer> 爪哇 - Invert Elements in ArrayList<Integer> Java 如何在二维ArrayList中的元素上添加(+)整数? - How can I add (+) an integer to an element in a 2-dimensional ArrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM