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

    Question Help me with this array element insertion

    i got an array {4,5,6,7};
    and i want to insert element 3 to index 2 ...
    so the array become like this {4,5,3,6,7} PLEASE HELP
    :confused::confused:

    my output is like this
    4
    5
    3
    7
    7
    <<wrong output ... please help me to check the error
    Java Code:
    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 !=location ; i--){
    				System.out.println("Entered the loop" +i);
    				a[i + 1] = a[i];
    				System.out.println("i+1" +i+1 + " a[i+1]"+a[i+1]+" == a[i]"+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

    Please don't multipost the same question more than once but rather edit your currently active thread. You've been notified on this before. Either that or let us know to delete it. I'm locking your other thread and will delete it in a bit.

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

    Default

    Your problem is where I've starred your code:
    Java Code:
    for (i = lastItem; i *** location; i--) {
    To figure out what conditional operators should go there you will want to walk through how you would do this on paper without a computer.

    Luck.

  4. #4
    siva_vs_siva is offline Member
    Join Date
    Aug 2010
    Posts
    31
    Rep Power
    0

    Default

    public static void main(String ar[])
    {
    int[] a=new int[5];
    a[0]=4;
    a[1]=5;
    a[2]=6;
    a[3]=7;
    int b,c;
    for(int i=0;i<a.length;i++)
    {
    if(i!=2)
    a[i]=a[i];
    if(i==2)
    {
    b=a[i];
    c=a[i+1];
    a[i]=3;
    a[i+1]=b;
    a[i+2]=c;

    }
    System.out.print(a[i]);
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  3. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  4. Unique element in an array
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 08:44 AM
  5. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 05:26 PM

Posting Permissions

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