Java ListIterator Usage differingDigits(final int lhs, final int rhs, final int base)

Here you can find the source of differingDigits(final int lhs, final int rhs, final int base)

Description

differing Digits

License

Open Source License

Declaration

public static int differingDigits(final int lhs, final int rhs,
            final int base) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Main {
    public static int differingDigits(final int lhs, final int rhs,
            final int base) {
        final List<Integer> lhsDigits = digits(lhs,
                new ArrayList<Integer>(), base);
        final List<Integer> rhsDigits = digits(rhs,
                new ArrayList<Integer>(), base);
        if (lhsDigits.size() != rhsDigits.size())
            return rhs;

        int index = 0;
        for (Iterator<Integer> rhsIt = rhsDigits.iterator(), lhsIt = lhsDigits
                .iterator(); rhsIt.hasNext(); index++)
            if (rhsIt.next() != lhsIt.next())
                return toInt(rhsDigits.subList(index, rhsDigits.size()),
                        base);/*from   ww  w. j  a va2s .  c  om*/

        return rhs;
    }

    public static List<Integer> digits(final int number,
            final List<Integer> digits, final int base) {
        final int digit = number % base;
        digits.add(0, digit);

        if (number == digit)
            return digits;
        else
            return digits((int) number / base, digits, base);
    }

    public static int toInt(final List<Integer> digits, final int base) {
        int pos = 1;
        int retVal = 0;
        for (ListIterator<Integer> it = digits.listIterator(digits.size()); it
                .hasPrevious();) {
            retVal += it.previous() * pos;
            pos = pos * base;
        }
        return retVal;
    }
}

Related

  1. castIterator(final Iterator iterator)
  2. clearNulls(Collection col)
  3. containsUsingListIteratorNext(List list, Integer value)
  4. createCommaSeparatedListOfFullTypeNames(ListIterator strIt)
  5. emptyIterator()
  6. filterStringList(final String prefix, final String infix, final String suffix, final List list)
  7. findFirstIterator(List list, int fromIndex, int toIndex, T value, Comparator comparator)
  8. findObject(Object item, ListIterator iter)