java - Having trouble getting each character from a string and add it to an ArrayList of integers sn:aware of bigInteger -


i having hard time trying come constructor example using array list.

public class bigintconstructordemo { public static void main(string[] args) {       bigint1 b1 = new bigint1("1000000");      system.out.println("b1 " + b1);     } } 

this class array called digits holds max of 40 digits. constructor loops through array....

public class bigint1 { public static final int max_digits = 40; private int [ ] digits = new int[max_digits];  public bigint1(string s){     (int = 0; < max_digits; i++)         digits [i] = 0;     int = 0;     for(int j = s.length()-1; j >= 0; j--){         digits[i] = s.charat(j) - '0';         i++;         }   } } 

and uses tostring method.

public string tostring(){     string s = "";       (i = 0; < max_digits;i++)          s = digits[i] + s;       if (s.equals(""))         s = "0";     return s; 

i know have use loop, can't seem syntax right. should use .add method comes array list class? how take single character in string , put each element of array list?

import java.util.arraylist;  public class bigint2{ private arraylist <integer> bignum = new arraylist<integer>();  public bigint() {  }  public bigint(string s){   for(int index = 0;index ? bignum.size();index++){      } 

yes, use add.

this old code array:

for(int j = s.length()-1; j >= 0; j--){         digits[i] = s.charat(j) - '0';         i++;         } 

with arraylist foreach get:

for (char c: s.tochararray()) {   digits.add(c - '0'); } 

note endianness (left right or right left) switched in case, if want other way around loop in previous example on s.tochararray() positions:

char[] chars = s.tochararray(); for(int j = chars.length-1; j >= 0; j--){         digits.add(chars[j] - '0');     } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -