0

I have javascript code like this:

var myArray = []; //integer
var anIndex = 1;
Array.prototype.delete = function (index) {
    var rest = this.slice(index + 1);
    this.length = index;
    return this.push.apply(this, rest);
};

and used like this:

myArray.delete(anIndex);

How to translate it into java code?

2
  • stackoverflow.com/a/15300432/3166303 Commented Sep 22, 2015 at 8:03
  • What's wrong with delete anArray[idx], or why not slice the array in de first place? Why augment the prototype (which is considered bad practice)? besides: questions asking to rewrite a snippet in another language are considered off-topic Commented Sep 22, 2015 at 8:04

1 Answer 1

0

What java collection are you using?

If you are using java.util.List, it has a method .remove(int index) that does what you require -- removes an element from array.

If you are using java arrays (e.g. int[]), consider using collections such as java.util.ArrayList instead, they have the methods you need.

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

Comments

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.