Reputation: 31
There is a problem in the output of my java code. In the tipOdd part, I want to remove the element in index 0 and odd index of an arraylist. For example, an arraylist [40 8 6 3 7 5 2], and then the expected output should be [6 7 2],but my code output is [6 3 7 5 2 6 7 2]. Can anyone can help me to solve the problem. I am very appreciate your help!
Code:
import java.io.*;
import java.util.*;
import java.util.ArrayList;
class ArrayLinearListRev extends ArrayLinearList{
public ArrayList<Integer> reverse(ArrayList<Integer> alist)
{
ArrayList<Integer> revArrayList = new ArrayList<Integer>();
for (int i = alist.size() - 1; i >= 0; i -- ) {
revArrayList.add(alist.get(i));
}
return revArrayList;
}
public void printElements(ArrayList<Integer> alist)
{
for (int i = 0; i < alist.size(); i++) {
System.out.print(alist.get(i) + " ");
}
}
public ArrayList<Integer> leftSh(int post, ArrayList<Integer> alist2)
{
ArrayList<Integer> LeftshifedList = new ArrayList<Integer>(alist2);
for (int i = 1; i <= post; i++)
{
LeftshifedList.remove(0);
}
return LeftshifedList;
}
public void printElements2(ArrayList<Integer> alist2)
{
for (int i = 0; i < alist2.size(); i++) {
System.out.print(alist2.get(i) + " ");
}
}
public ArrayList<Integer> tipOdd(ArrayList<Integer> alist3)
{
ArrayList<Integer> removeOdd = new ArrayList<Integer>(alist3);
for (int i = 0; i < alist3.size(); i++)
{
if (i == 0 || i%2 == 0){
removeOdd.add(alist3.get(i));
}
}
return removeOdd;
}
public void printElements3(ArrayList<Integer> alist3)
{
for (int i = 0; i < alist3.size(); i++) {
System.out.print(alist3.get(i) + " ");
}
}
}
public class ArrayLinearListFun
{
public static void main(String[] args)
{
ArrayLinearListRev obj = new ArrayLinearListRev();
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(0, new Integer(2));
x.add(1, new Integer(5));
x.add(2, new Integer(7));
x.add(3, new Integer(3));
x.add(4, new Integer(6));
x.add(5, new Integer(8));
x.add(6, new Integer(40));
System.out.print("The list is: ");
obj.printElements(x);
x = obj.reverse(x);
System.out.print("\nThe list is: ");
obj.printElements(x);
x=obj.leftSh(2, x);
System.out.print("\nThe list is: ");
obj.printElements2(x);
x=obj.tipOdd(x);
System.out.print("\nThe list is: ");
obj.printElements3(x);
}
}
Upvotes: 0
Views: 187
Reputation: 86120
I have not gone through all of your code. But concerning your tipOdd
method: You first create a new list, removeOdd
containing all the elements from the original list, alist3
. If the original list was [10, 2, 3], then the new list is [10, 2, 3] from the outset. Next you add to the new list all the elements in even indices in the original list. So [10, 2, 3] gets expanded to [10, 2, 3, 10, 3].
Instead I suggest you start out from an empty list. Just remove the argument to new ArrayList<Integer>
in the first line of the method. Then to this empty list add the elements that you want to keep from the original list.
Your condition i == 0 || i%2 == 0
may not be quite what you want. When i
is 0, i % 2
is 0 too, so the first part makes no difference. If you did not want the element at index 0 included in the new list, consider i != 0 && i % 2 == 0
.
Upvotes: 1