Results 1 to 3 of 3
- 09-06-2010, 12:49 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 49
- Rep Power
- 0
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]);
}
}
}
-
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 [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Luck.
-
question multiposted. Locking this thread and will delete it soon.
Similar Threads
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Copy a specified element from one array list to another arraylist
By TaxpayersMoney in forum New To JavaReplies: 1Last Post: 05-20-2010, 10:17 PM -
Inserting input into array
By brmcdani in forum New To JavaReplies: 3Last Post: 02-05-2010, 02:33 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
adding list to an array element
By Preethi in forum New To JavaReplies: 5Last Post: 09-25-2008, 04:23 AM


LinkBack URL
About LinkBacks


Bookmarks