【问题标题】:How to check Int[] in an arraylsit如何检查数组列表中的 Int[]
【发布时间】:2015-05-24 14:44:25
【问题描述】:

当我将它传递给构造函数时:{12,1,3,22,0,4}

public class Test 
{
private ArrayList<int[]> arraylistone;

public Test(int[] ab) //todo
{
    int[] xy = new int[2];
    arraylistone = new ArrayList<int[]>(ab.length);

    for(int i=0; i < ab.length; i++){  
        int sv = String.valueOf(ab[i]).length();
        if (sv == 1){

            xy[0] = 0;
            xy[1] = ab[i];
            arraylistone.add(xy);
        }
        else if(sv == 2){
            //TODO
        }
        else{
            System.out.println("errormessage");
            //throw argument "too many digits..."
        } 
        System.out.println(arraylistone); // want to check that the array list ab has been added converterted into two single digit numbers (xy) and stored in  arraylistone
    }
}

}

  1. 如何检查它是否已正确添加到arraylistone?我想打印出它的值以确保我在正确的轨道上,我是否应该使用toString() 以某种方式将数组对象转换为String?现在它会打印出类似这样的内容 `"[I@72d67791][I@72d67791][I@72d67791]"

"`

  1. 如果(sv == 2) 我将如何获得String 中每个intint 值?例如给定int 12,如果我要将其分成单独的xy = int[],其中xy[0] =1xy[1]=2

【问题讨论】:

  • 请发布可编译并正确缩进的代码。阅读docs.oracle.com/javase/8/docs/api/java/util/…
  • 为什么将整数数组传递给构造函数并重新分配其内存?
  • 我建议您重新开始,写下您想在 cmets 中执行的操作,然后尽可能少写几行代码。你现在有很多错误和无意义的代码。
  • 好的,抱歉,我已经确保这段代码编译并修复了明显令人困惑的错误代码......只是为了确保我清楚 arraylistone[0] 应该持有并且 int[] xy 其中 xy[0] = 1 和 xy[1] = 2。我意识到我还没有完成 2 位部分,但我只是想在继续之前检查 arraylistone 的各个元素

标签: java arraylist arrayobject


【解决方案1】:

我认为这会让你大吃一惊。老师一定会喜欢这样的:D:

public Test(@NotNull int[] ab) //todo   
{
   List<int[]> arraylistone = new ArrayList<int[]>(ab.length) {
            @Override
            public String toString() {
                StringBuilder sb = new StringBuilder();
                for (int[] ints : this) {
                    sb.append(Arrays.toString(ints));
                }
                return sb.toString();
            }
        }; 

   System.out.println("how many strings "+ab.length); //testing

   for (int i = 0; i < ab.length; ++i) {
       int sv = String.valueOf(ab[i]).length();
       int[] xy = new int[sv];
       for (int t, base = 10, s = 0; s < xy.length; ++s) {
           here:{
                t = ab[i] / (int) Math.pow(base, --sv);
                if (t / base == 0) break here;
                t %= t / base * base;
           } xy[s] = t;
       }
       arraylistone.add(xy);
   }

   System.out.println(arraylistone); // want to check that the array list ab has been added to arraylistone        
}

【讨论】:

  • 不是真的在让我大吃一惊的东西或给老师留下深刻印象的东西之后……我想学习,不过感谢您的意见
  • 开个玩笑。我想所有的观众都明白你现在才刚刚开始。祝你好运。
猜你喜欢
  • 2015-01-16
  • 2013-04-08
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多