nsing991
asked on
How do I reverse the order of integer elements in an ArrayList?
How do I reverse the order of integer elements in an ArrayList? I have an ArrayList with integer elements. I am trying to print the elements so that the last element is the first printed. For the below code segment if I have an ArrayList like: [2, 7,3,1] and am printing them in a frame using a loop so that the ouput is:
Reference String Value 2: 2 -1 -1 -1
Reference String Value 7: 2 7 -1 -1
Reference String Value 3: 2 7 3 -1
Reference String Value 1: 2 7 3 1
I want it to print as:
Reference String Value 2: 2 -1 -1 -1
Reference String Value 7: 7 2 -1 -1
Reference String Value 3: 3 7 2 -1
Reference String Value 1: 1 3 7 2
referenceString is the ArrayList in the code.
Reference String Value 2: 2 -1 -1 -1
Reference String Value 7: 2 7 -1 -1
Reference String Value 3: 2 7 3 -1
Reference String Value 1: 2 7 3 1
I want it to print as:
Reference String Value 2: 2 -1 -1 -1
Reference String Value 7: 7 2 -1 -1
Reference String Value 3: 3 7 2 -1
Reference String Value 1: 1 3 7 2
referenceString is the ArrayList in the code.
case 4:
System.out.println();
int count4 = 0;
int frame[] = new int[PHYSICAL_FRAMES];
for(int i = 0; i < PHYSICAL_FRAMES; i++){
frame[i] = -1;
}
do{
int pg = 0;
while(pg < stringLength){
page = referenceString.get(pg);
pg++;
flag = true;
for(int j = 0; j < PHYSICAL_FRAMES; j++){
if(page == frame[j]){
flag = false;
break;
}
}
if(flag){
frame[count4] = page;
count4++;
if(count4 == PHYSICAL_FRAMES)
count4 = 0;
System.out.print("Reference String Value " + page + ": ");
for(int j = 0; j < PHYSICAL_FRAMES; j++)
System.out.print(frame[j] + " ");
System.out.println();
pageFault++;
}
else{
System.out.print("frame :");
for(int j = 0; j < PHYSICAL_FRAMES; j++)
System.out.print(frame[j]+" ");
System.out.println();
}
chn++;
}
}
while(chn != stringLength);
System.out.println("Page fault:" + pageFault);
System.out.println();
break;
It is hard to undersatnd your example, but if you want to print the ArrayList in the reverse order,
then the above code will do it for you
then the above code will do it for you
if you want to reverse just the segement of your array list:
for(int j=0; j<num; j++){
System.out.printn(arraylist.get(num-1 -j));
}
That would print in the way you wanted many times :for(int i=0; i<arraylist.size(); i++){
for(int j=0; j<i; j++){
System.out.print(arraylist.get(i-1 -j) + " ");
}
for(int j=i; i<arraylist.size(); j++){
System.out.print("-1 ");
}
System.out.println("");
}
corrected a few mistakes - this works:
ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add(2);
arraylist.add(7);
arraylist.add(3);
arraylist.add(1);
for(int i=0; i<arraylist.size(); i++){
for(int j=0; j<i; j++){
System.out.print(arraylist.get(i-1 -j) + " ");
}
for(int j=i; j<arraylist.size(); j++){
System.out.print("-1 ");
}
System.out.println("");
}
Output from the above
-1 -1 -1 -1
2 -1 -1 -1
7 2 -1 -1
3 7 2 -1
Now it is whifted by one - exactly as you wanted (see output below):
ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add(2);
arraylist.add(7);
arraylist.add(3);
arraylist.add(1);
for(int i=1; i<arraylist.size()+1; i++){
for(int j=0; j<i; j++){
System.out.print(arraylist.get(i-1 -j) + " ");
}
for(int j=i; j<arraylist.size(); j++){
System.out.print("-1 ");
}
System.out.println("");
}
2 -1 -1 -1
7 2 -1 -1
3 7 2 -1
1 3 7 2
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thank you. This actually fits more in the context of what I wanted it to do. I'm trying to simulate demand paging using this first in first out algorithm. I will have to tweek some things but this helps tremendously.
In your code you are actually using not ArrayList
but array of ints,
so this is with array
but array of ints,
so this is with array
int [] frame = {2,7,3,1};
for(int i=1; i<frame.length+1; i++){
System.out.print("Reference string value " + frame[i-1] + ": ");
for(int j=0; j<i; j++){
System.out.print(frame[i-1 -j] + " ");
}
for(int j=i; j<frame.length; j++){
System.out.print("-1 ");
}
System.out.println("");
}
Reference string value 2: 2 -1 -1 -1
Reference string value 7: 7 2 -1 -1
Reference string value 3: 3 7 2 -1
Reference string value 1: 1 3 7 2
ASKER
Ahhh. Thanks for pointing that out. I'm using an ArrayList elsewhere in my code but for this particular segment it is an array.
yes, but as you see the difference is of course only in some formal syntax
Open in new window