Results 1 to 3 of 3
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default Trying to make an array list // inserting an element to middle of array

    Please help ,, to insert an element in the middle of an array , we need to shift all elements , and then insert the element we want into our location,, please check my code,,,, and help me please !!


    LET US ASSUME THAT THE ARRAY SIZE IS SUFFICIENT <<
    public class List {

    int []a = new int[5];
    int lastItem;



    public List(){

    a[0] = 4;
    a[1] = 5;
    a[2] = 6;
    a[3] = 7;
    lastItem = a.length -2;

    }

    public void insertItem(int newItem, int location){
    int i;
    System.out.println("the last item "+lastItem);
    for(i = lastItem; i < a.length; i--){
    a[i + 1] = a[i];
    }

    a[location] = newItem;
    lastItem++;

    for(int m = 0 ; m < a.length; m++){
    System.out.println(a[m]);
    }

    }



    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by javanew View Post
    Please help ,, to insert an element in the middle of an array , we need to shift all elements , and then insert the element we want into our location,, please check my code,,,, and help me please !!
    Please first help us by
    1) asking an answerable question -- what is the matter with your current code for instance? Are you getting errors? Wrong output? Don't assume that we know anything about the results you're getting, and
    2) using code tags: when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

    Luck.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  2. Replies: 1
    Last Post: 05-20-2010, 11:17 PM
  3. Inserting input into array
    By brmcdani in forum New To Java
    Replies: 3
    Last Post: 02-05-2010, 03:33 PM
  4. Replies: 1
    Last Post: 03-31-2009, 07:40 AM
  5. adding list to an array element
    By Preethi in forum New To Java
    Replies: 5
    Last Post: 09-25-2008, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •