存储为字符类型数组列表的大量数字的乘法

时间:2019-03-07 16:19:51

标签: java arraylist multiplication

 private ArrayList<Character> product(ArrayList<Character> list1, ArrayList<Character> list2) {
        int size1 = list1.size();
        int size2 = list2.size();
        ArrayList<Character> list3 = new ArrayList<Character>();
        ArrayList<Character> list4 = new ArrayList<Character>();
        for (int index = (size2 -1); index >= 0; index--) {
            int carryOver = 0;
            int charInt1 = (int) ( list2.get(index) -'0');
            int count = index;
            while(count > 0) {
                list3.add('0');
                count--;
            }
            for ( int key = 0; key < size1; key++) {
                int charInt2 = (int) ( list1.get(key) - '0');
                int product = (charInt1 * charInt2) + carryOver;
                char intChar = (char) ( (product % 10) + 48);
                list3.add(intChar);
                carryOver = product / 10;

            }
            if ( carryOver != 0) {
                char carryChar = (char) ( carryOver + 48);
                list3.add(carryChar);
            }

            list4 = sum (list3, list4);
            list3.clear();

        }
        return list4;
    }

这是用于存储在Character类型的ArrayList中的大量数字的乘法。这段代码不适用于32 * 32、91 * 11这样的情况。方法sum可以正常工作。

0 个答案:

没有答案