-1

When i pass this to the constructor: {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. How do I check that it has been added to arraylistone correctly? I want to print out the value of it to make sure I'm on the right track, should i use toString() somehow to convert the an array object into a String? As it sits now it prints out something like this `"[I@72d67791][I@72d67791][I@72d67791]"

"`

  1. If (sv == 2) how would I get the int value of each individual int in the String? For example given the int 12, if I was to divide that into a separate xy = int[] where xy[0] =1 and xy[1]=2
30iT
  • 125
  • 1
  • 1
  • 7
  • 6
    Please post code that compiles, and is properly indented. Read https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-int:A- – JB Nizet May 24 '15 at 14:47
  • Why did you pass an integer array into the constructor and reallocate its memory ? – felix.infinite May 24 '15 at 15:18
  • I'd advice you to start over, write down what you want to do in comments and then write the code in as few lines as possible. You have a lot of erroneous and nonsensical code right now. – Jos van Egmond May 24 '15 at 15:21
  • OK my apologies, i have made sure this code compiles and fixed up the obviously confusing code which was wrong...just to make sure i'm clear arraylistone[0] should hold and int[] xy where xy[0] = 1 and xy[1] = 2. i realize i haven't done the 2 digit part yet i was just trying to check the individual elements of arraylistone before proceeding – 30iT May 25 '15 at 08:08

1 Answers1

-1

I think this is something that will blow up your mind. A teacher will definitely like this :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        
}
Aliaksei Yatsau
  • 749
  • 7
  • 12
  • not really after blow my mind stuff or something to impress teacher...im looking to learn, thanks for your input though – 30iT May 25 '15 at 08:21
  • It's just a joke. I think all viewers understand that u are now in the very beginning. Wish u good luck. – Aliaksei Yatsau May 26 '15 at 08:47