简体   繁体   English

如何检查arraylist的int []类型的对象?

[英]How to check the object of type int[] of an arraylist?

I'm trying to find out particular elements in my ArrayList (arrayOne). 我试图找出我的ArrayList (arrayOne)中的特定元素。 Each element should be an int[] . 每个元素应该是一个int[] I've tried System.out.println(arrayOne) , which compiles but gives a irregular and strange number "[[I@370968]". 我试过了System.out.println(arrayOne) ,它可以编译但给出不规则且奇怪的数字“ [[I @ 370968]””。

I've also tried System.out.println(arrayOne[0]) but it won't compile and emits the error 我也尝试过System.out.println(arrayOne[0])但它不会编译并发出错误

Array required but java.util.ArrayList found. 需要数组,但找到了java.util.ArrayList。

Given is the following code, with {1,12,3,13,123,2} passed to eg : 给定以下代码,并将{1,12,3,13,123,2}传递给eg

import java.util.ArrayList;

public class arrayTest {
    private ArrayList<int[]> arrayOne; 

    public arrayTest(int[] eg) {
        int[] xy = new int[2];
        arrayOne = new ArrayList<int[]>(eg.length);
        for (int i = 0; i < eg.length; i++) {
            int sv = String.valueOf(eg[i]).length();
            if (sv == 1) {
                xy[0] = 0;
                xy[1] = eg[i];
                arrayOne.add(xy);
            }
            else if (sv == 2) {
                System.out.println("two digits");
                // TODO add code to make xy[0] = the first
                // digit of eg and xy[1] = the second digit
            }
            else {
                System.out.println("too many digits");
                // and throw error accordingly
            }
            System.out.println(arrayOne);
        }
    }
}
  1. How do make sure and print out the int array at arrayOne[0] 如何确定并在arrayOne [0]上打印出int数组
  2. Given the code above if (sv == 2) and i want to split each individual number into an int[] with [0] being the first digit and [1] being the second digit how would i get the int value of each individual digit. 给定上面的代码,如果(sv == 2)并且我想将每个数字拆分为一个int [],其中[0]是第一个数字,[1]是第二个数字,我将如何获取每个个体的int值数字。

Use Arrays.toString(yourArray); 使用Arrays.toString(yourArray); to print out arrays in human readable form. 以人类可读的形式打印数组。

First of all, the string [I@370968 is displayed because you are trying to print an int[] , which is actually an object. 首先,显示字符串[I@370968 ,因为您尝试打印int[] ,它实际上是一个对象。 Because this object does not override the object's toString() method, that method is derived from the Object class. 因为此对象未覆盖对象的toString()方法,所以该方法是从Object类派生的。 The Object.toString() implementation, which prints the class name (in this case [I , because it is an int array), then an @ sign, and then the hash code of the object. Object.toString()实现,它打印类名(在本例中为[I ,因为它是一个int数组),然后打印一个@符号,然后打印该对象的哈希码。

Your ArrayList contains a number of int[] s. 您的ArrayList包含许多int[] Because an ArrayList is not an array (the one with the square brackets, like int[] ), you can't call an element on it as if it were an array. 因为ArrayList不是数组(带有方括号的数组,例如int[] ),所以不能像在数组上那样调用元素。 In short, you cannot call arrayOne[someDesiredIndex] . 简而言之,您不能调用arrayOne[someDesiredIndex]

In order to get an element from the ArrayList, call get(int index) on it; 为了从ArrayList中获取一个元素,在其上调用get(int index) ; it returns the desired int[] . 它返回所需的int[] As already pointed out by another answer, you can use Arrays.toString(int[]) to print it in a human readable form. 正如另一个答案所指出的那样,您可以使用Arrays.toString(int[])以易于阅读的形式打印它。

To answer your questions: 要回答您的问题:

  1. You can retrieve the first index ( 0 ) of the first array inside arrayOne with the following code: arrayOne.get(0)[0] . 可以检索第一索引( 0内的第一阵列的) arrayOne用下面的代码: arrayOne.get(0)[0]
  2. The following code should work: 下面的代码应该工作:

     private static int[] intToArray(int n) { String str = String.valueOf(n); int length = str.length(); int[] ints = new int[length]; for (int i = 0; i < length; i++) { ints[i] = Integer.parseInt(str.substring(i, i + 1)); } return ints; } 

    Above method puts each digit into the next array position (it also works with digits greater than 99). 上面的方法将每个数字放入下一个数组位置(它也适用于大于99的数字)。 With this method you can easily get each individual digit: 使用此方法,您可以轻松获取每个数字:

     int[] digits = intToArray(47); int a = digits[0]; // Will be 4 int b = digits[1]; // Will be 7 

So this is the class rewritten: 所以这是重写的类:

public class Rewrite {

    private ArrayList<int[]> arrayOne = new ArrayList<int[]>();

    public Rewrite(int[] eg) {
        for (int i = 0; i < eg.length; i++) {
            int length = String.valueOf(eg[i]).length();
            switch (length) {
                case 1:
                    this.arrayOne.add(new int[] { 0, eg[i] });
                    break;
                case 2:
                    this.arrayOne.add(intToArray(eg[i]));
                    break;
                default:
                    throw new IllegalArgumentException("Number " + eg[i] + " has too many digits");
                    // Or display the error or something.
            }
            System.out.println(Arrays.toString(this.arrayOne.get(i)));
        }
    }

    private static int[] intToArray(int n) {
        String str = String.valueOf(n);
        int length = str.length();
        int[] ints = new int[length];
        for (int i = 0; i < length; i++) {
            ints[i] = Integer.parseInt(str.substring(i, i + 1));
        }
        return ints;
    }

    public static void main(String[] args) {
        Rewrite r = new Rewrite(new int[] { 47, 53, 91, 8 });
    }

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

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