Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What is the significance of array push Java?

Asked by Nada Oct 12, 2022 535 views 1 answer
Share

About this question

 I want to make a method arrayIntPush(3, {5,4,8}) that will return {5,4,8,3}. This method seems to work:

public static int[] arrayIntPush(int item, int[] oldArray) {

    int len = oldArray.length;
    int[] newArray = (int[]) Array.newInstance(oldArray.getClass().getComponentType(), len + 1);
    System.arraycopy(oldArray, 0, newArray, 0, len);
    System.arraycopy(new int[] {item}, 0, newArray, len, 1);
return newArray;
}

Is there a better way to do this that would be faster?

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.