1

I have some reference code which is written in Javascript and I am trying to port it to Java.

The problem I have with the porting is that I don't have anything in Java which can be used as stack and normal array at the same time. The reference code uses an array object which can act as a normal array and also like a stack.

Is there anything which I can use to resolve my problem?

Reference Code:

var ascii85 = function (input) {
    // input: Array: an array of numbers (0-255) to encode
    var result = [],
        reminder = input.length % 4,
        length = input.length - reminder;
    c(input, length, result);
    if (reminder) {
        var t = input.slice(length);
        while (t.length < 4) {
            t.push(0);
        }
        c(t, 4, result);
        var x = result.pop();
        if (x == "z") {
            x = "!!!!!";
        }
        result.push(x.substr(0, reminder + 1));
    }
    return result.join("");
};

The code of c function is below:

var c = function (input, length, result) {
    var i, j, n, b = [0, 0, 0, 0, 0];
    for (i = 0; i < length; i += 4) {
        n = ((input[i] * 256 + input[i + 1]) * 256 + input[i + 2])
            * 256 + input[i + 3];
        if (!n) {
            result.push("z");
        } else {
            for (j = 0; j < 5;
                b[j++] = n % 85 + 33, n = Math.floor(n / 85)
            );
        }
        result.push(String.fromCharCode(
            b[4], b[3], b[2], b[1], b[0]));
    }
};
3
  • 1
    Did an old school C programmer write that code? It's not very...JavaScript-y. Commented Apr 5, 2015 at 19:35
  • Also "stack and normal array at the same time" - I'm trying to see what of both is needed here, but until then on a hunch - would a List work? It implements the Stack interface and you can do list.get(n). Commented Apr 5, 2015 at 19:37
  • Let me give it a try using a List. Commented Apr 5, 2015 at 19:40

1 Answer 1

1

Javascript Array.prototype.push corresponds to List#add:

t.add(0);

List has no analog for Array.prototype.pop but you can remove the last element:

result.remove(t.size() - 1);

So the code could therefore be converted to use ArrayList, for example.

LinkedList allows push/pop as well as access by index but its indexing is O(n).

There is also Stack which has push/pop and O(1) indexing, but it's a subclass of Vector which new code is generally steered away from using. (Basically Vector's operations are synchronized for thread safety, which is a pointless overhead if the Stack isn't shared between threads. Vector is superseded by Collections.synchronizedList in this regard, so Stack has a very niche feature set.)

I also don't see a problem with doing something like the following:

public class MyList<E> extends ArrayList<E> {
    public E pop() {
        return this.remove(this.size() - 1);
    }
}

It doesn't break Liskov Substitution Principle (still behaves as an ArrayList).

Sign up to request clarification or add additional context in comments.

3 Comments

Sorrry, yes - I was looking at a different implementation of List
I think Vid's solution is also useful but have not tried it but I think it should also work. Thanks for your solutions too. :)
@Sunny I've mentioned Stack in my answer. Of course whatever you do is up to you. ; )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.