-1

当我将它传递给构造函数时:{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)我将如何获得int每个人int的价值String?例如给定int12,如果我要将它分成单独的xy = int[]wherexy[0] =1xy[1]=2
4

1 回答 1

-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-05-24T15:14:49.220 回答