简体   繁体   English

java 中 int 数组的 ArrayList

[英]ArrayList of int array in java

I'm new to the concept of arraylist.我是 arraylist 概念的新手。 I've made a short program that is as follows:我制作了一个简短的程序,如下所示:

ArrayList<int[]> arl=new ArrayList<int[]>();
int a1[]={1,2,3};
arl.add(0,a1);
System.out.println("Arraylist contains:"+arl.get(0));

It gives the output: Arraylist contains:[I@3e25a5它给出了 output: Arraylist contains:[I@3e25a5

Now my questions are:现在我的问题是:

  1. How to display the correct value ie 1 2 3.如何显示正确的值,即 1 2 3。
  2. How can I access the single element of array a1 ie if I want to know the value at a1[1].我如何访问数组 a1 的单个元素,即如果我想知道 a1[1] 的值。

First of all, for initializing a container you cannot use a primitive type (ie int ; you can use int[] but as you want just an array of integers, I see no use in that).首先,对于初始化容器,您不能使用原始类型(即int ;您可以使用int[]但因为您只需要一个整数数组,我认为这没有用)。 Instead, you should use Integer , as follows:相反,您应该使用Integer ,如下所示:

ArrayList<Integer> arl = new ArrayList<Integer>();

For adding elements, just use the add function:要添加元素,只需使用add函数:

arl.add(1);  
arl.add(22);
arl.add(-2);

Last, but not least, for printing the ArrayList you may use the build-in functionality of toString() :最后但并非最不重要的是,为了打印ArrayList您可以使用toString()的内置功能:

System.out.println("Arraylist contains: " + arl.toString());  

If you want to access the i element, where i is an index from 0 to the length of the array-1, you can do a :如果要访问i元素,其中i是从 0 到数组长度的索引,您可以执行以下操作:

int i = 0; // Index 0 is of the first element
System.out.println("The first element is: " + arl.get(i));

I suggest reading first on Java Containers, before starting to work with them.我建议先阅读 Java Containers,然后再开始使用它们。

  1. 使用Arrays.toString( arl.get(0) )

  2. arl.get(0)[1]

More simple than that.比这更简单。

List<Integer> arrayIntegers = new ArrayList<>(Arrays.asList(1,2,3));

arrayIntegers.get(1);

In the first line you create the object and in the constructor you pass an array parameter to List.在第一行中创建对象并在构造函数中将数组参数传递给 List。

In the second line you have all the methods of the List class: .get (...)在第二行中,您拥有 List 类的所有方法: .get (...)

The setup:设置:

    List<int[]> intArrays=new ArrayList<>();
    int anExample[]={1,2,3};
    intArrays.add(anExample);

To retrieve a single int[] array in the ArrayList by index:要按索引检索 ArrayList 中的单个 int[] 数组

    int[] anIntArray = intArrays.get(0); //'0' is the index
    //iterate the retrieved array an print the individual elements
    for (int aNumber : anIntArray ) { 
        System.out.println("Arraylist contains:" + aNumber );
    }

To retrieve all int[] arrays in the ArrayList:要检索 ArrayList 中的所有 int[] 数组

    //iterate the ArrayList, get and print the elements of each int[] array  
    for(int[] anIntArray:intArrays) {
       //iterate the retrieved array an print the individual elements
       for (int aNumber : anIntArray) {
           System.out.println("Arraylist contains:" + aNumber);
       }
}

Output formatting can be performed based on this logic.可以基于此逻辑执行输出格式化。 Goodluck!!祝你好运!!

In java, an array is an object.在java中,数组是一个对象。 Therefore the call to arl.get(0) returns a primitive int[] object which appears as ascii in your call to System.out.因此,对 arl.get(0) 的调用将返回一个原始 int[] 对象,该对象在您对 System.out 的调用中显示为 ascii。

The answer to your first question is therefore因此,您的第一个问题的答案是

System.out.println("Arraylist contains:"+Arrays.toString( arl.get( 0 ) ) );

If you're looking for particular elements, the returned int[] object must be referenced as such.如果您正在寻找特定元素,则必须如此引用返回的 int[] 对象。 The answer to your second question would be something like你的第二个问题的答案是这样的

    int[] contentFromList = arl.get(0);
    for (int i = 0; i < contentFromList.length; i++) {
        int j = contentFromList[i];
        System.out.println("Value at index - "+i+" is :"+j);
    }

You have to use <Integer> instead of <int> :您必须使用<Integer>而不是<int>

int a1[] = {1,2,3};
ArrayList<Integer> arl=new ArrayList<Integer>();
for(int i : a1) {
    arl.add(i);        
    System.out.println("Arraylist contains:" + arl.get(0));
}

Everyone is right.每个人都是对的。 You can't print an int[] object out directly, but there's also no need to not use an ArrayList of integer arrays.你不能直接打印一个 int[] 对象,但也没有必要不使用整数数组的 ArrayList。

Using,使用,

Arrays.toString(arl.get(0))

means splitting the String object into a substring if you want to insert anything in between, such as commas.意味着如果您想在中间插入任何内容(例如逗号),则将 String 对象拆分为一个子字符串。

Here's what I think amv was looking for from an int array viewpoint.这是我认为 amv 从 int 数组的角度寻找的东西。

System.out.println("Arraylist contains: " 
    + arl.get(0)[0] + ", " 
    + arl.get(0)[1] + ", " 
    + arl.get(0)[2]);

This answer is a little late for amv but still may be useful to others.这个答案对 amv 来说有点晚了,但仍然可能对其他人有用。

java.util.Arrays.toString()将 Java 数组转换为字符串:

System.out.println("Arraylist contains:"+Arrays.toString(arl.get(0)));
ArrayList<Integer> list = new ArrayList<>();
int number, total = 0;

for(int i = 0; i <= list.size(); i++){
    System.out.println("Enter number " + (i + 1) + " or enter -1 to end: ");
    number = input.nextInt();

    list.add(number);

    if(number == -1){
        list.remove(list.size() - 1);
        break;
    }
}
System.out.println(list.toString());

for(int i: list){
    System.out.print(i + "  ");
    total+= i;
}
System.out.println();
System.out.println("The sum of the array content is: " + total);

Integer 是包装类,int 是原始数据类型。总是喜欢在 ArrayList 中使用 Integer。

For the more inexperienced, I have decided to add an example to demonstrate how to input and output an ArrayList of Integer arrays based on this question here .对于更没有经验,我决定添加一个例子来说明如何输入和输出基于这个问题整型数组的ArrayList 这里

    ArrayList<Integer[]> arrayList = new ArrayList<Integer[]>();
    while(n > 0)
    {
        int d = scan.nextInt();
       Integer temp[] = new Integer[d];
        for (int i = 0 ; i < d ; i++)
        {
            int t = scan.nextInt();
            temp[i]=Integer.valueOf(t);
        }
        arrayList.add(temp);
        n--;
    }//n is the size of the ArrayList that has been taken as a user input & d is the size 
    //of each individual array.

     //to print something  out from this ArrayList, we take in two 
    // values,index and index1 which is the number of the line we want and 
    // and the position of the element within that line (since the question
    // followed a 1-based numbering scheme, I did not change it here)

    System.out.println(Integer.valueOf(arrayList.get(index-1)[index1-1]));

Thanks to this answer on this question here , I got the correct answer.感谢这里对这个问题的回答,我得到了正确的答案。 I believe this satisfactorily answers OP's question, albeit a little late and can serve as an explanation for those with less experience.我相信这令人满意地回答了 OP 的问题,尽管有点晚,并且可以作为对经验较少的人的解释。

List integerList = IntStream.range(0,100).boxed().toList();列表 integerList = IntStream.range(0,100).boxed().toList();

This is one of the ways, you can initialize the fixed size ArrayList in Java using Java8 - Stream API.这是其中一种方式,可以使用Java8在Java中初始化固定大小的ArrayList - Stream ZDB143442381D0Z8ACE8 This List is going to contain integer values from 0 to 99.此列表将包含从 0 到 99 的 integer 值。

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

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