Question

Implement a Paritylterator for an ArrayList such that when the arraylist has odd number of items, it will only return the 1st, 3rd, 5th, items and if the arraylist has even number of items it will return the 2nd, 4th, 6th... items.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

public class ParityIterator {

public static <T> void parity(ArrayList<T> list)

{

int size=list.size();

if(size%2==0)

{

for(int i=0;i<size;i+=2)

System.out.println(list.get(i));

}

else

{

for(int i=1;i<size;i+=2)

System.out.println(list.get(i));

}

}

public static void main(String[] args) {

ArrayList<String> s=new ArrayList<String>();

ArrayList<String> l=new ArrayList<String>();

s.add("A");

s.add("B");

s.add("C");

s.add("D");

s.add("E");

s.add("F");

l.add("A");

l.add("B");

l.add("C");

l.add("D");

l.add("E");

parity(s);

parity(l);

}

}

Please do give a thumbs up and leave a comment if you have any doubt.

Add a comment
Know the answer?
Add Answer to:
Implement a Paritylterator for an ArrayList such that when the arraylist has odd number of items,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hame manual transmission: 1st-3.24, 2nd - 2.10, 3rd - 1.42, 4th - 1.00, 5th - 0.81...

    Hame manual transmission: 1st-3.24, 2nd - 2.10, 3rd - 1.42, 4th - 1.00, 5th - 0.81 and 6th - 0.62. The differential has a ratio of 3.55:1 (the tire spins slower than the output of the transmission). The rear tire radius is 13.63 inches and the engine's maximum speed is 7500rpm. If the car is travelling at 105mph, what is the lowest selectable gear by number and corresponding engine speed? A 2018 Mustang GT has the following gear ratios in...

  • Write a program that inputs a number between 1 and 10 and displays the number with...

    Write a program that inputs a number between 1 and 10 and displays the number with the appropriate two-letter ending (e.g., 1st, 2nd, 3rd, 4th, 5th, . . . ). Using Visual Logic

  • Write a Java method that takes a String as its input and prints on the first...

    Write a Java method that takes a String as its input and prints on the first line the odd characters (1st, 3rd, etc) and on the second line the even characters (2nd, 4th, etc).

  • I need help writing a program for this assignment Problem: Locker puzzle A school has 100...

    I need help writing a program for this assignment Problem: Locker puzzle A school has 100 lockers and 100 students. All lockers are locked on the first day of school. As the students enter, the 1st student opens every locker. Then the 2nd student, begins with the 2nd locker, closes every other locker. The 3rd student begins with the 3rd locker and changes every 3rd locker (closes it if it's open and opens it if its closed). The 4th student...

  • 5. Choose the best answer Calculate the future value of the following variable and ordinary annuities,...

    5. Choose the best answer Calculate the future value of the following variable and ordinary annuities, if the interest rate is 7% for all 1st year annuity: 10,000 2nd Annuity: 8000 3rd Annuity: 6000 4th Annuity: 5000 Annuity 5th: 4000 6th Annuity: 3000 Annuity 7th: 2000 Possible answers: $ 47,087.40 $ 45,087.40 $ 48,087.40 $ 44,087.40

  • Sweaters sell for $23 at the crafts fair. Allie knits sweaters and her marginal costs are...

    Sweaters sell for $23 at the crafts fair. Allie knits sweaters and her marginal costs are given in the table below. Allie’s marginal costs increase with each additional sweater. If Allie is behaving rationally, how many sweaters will she sell? 1st sweater 6 2nd sweater 9 3rd sweater 13 4th sweater 19 5th sweater 26 6th sweater 33

  • Sweaters sell for $20 at the crafts fair. Allie knits sweaters and her marginal costs are...

    Sweaters sell for $20 at the crafts fair. Allie knits sweaters and her marginal costs are given in the table below. Allie’s marginal costs increase with each additional sweater. If Allie is behaving rationally, how many sweaters will she sell? Marginal Cost ($) 1st sweater 3 2nd sweater 6 3rd sweater 10 4th sweater 16 5th sweater 23 6th sweater 30

  • 39) What are the units of k in the following rate law? Rate = K[X][Y]1/2 40)...

    39) What are the units of k in the following rate law? Rate = K[X][Y]1/2 40) What are the units of k in the following rate law? Rate = k[X]2 41) What is the overall order of the following reaction, given the rate law? X + 2Y → 42 Rate = k[X][Y] A) 3rd order B) 5th order C) 2nd order D) 1st order E) 6th order 42) What is the overall order of the following reaction, given the rate...

  • Suppose that Jacob spends his entire (I) of $100 on concerts (C) and movies (M). The...

    Suppose that Jacob spends his entire (I) of $100 on concerts (C) and movies (M). The prices of a concert (Pc) and movie (Pm) are $20 and $10, respectively. The following table summarizes the data on his marginal utity per dollar (MU/P) for each unit of concerts and movies. ((Concerts (C) (Pc = $20) ((Movies (M) (Pm = $10) C MUc MUc/Pc M MUm MUm/Pm 1st 100 1st 60 2nd 80 2nd 45 3rd 60 3rd 40 4th 40 4th...

  • answer final mass of unknown mass % water in unknown final mass of unknown Initial observations of your unknown: 2...

    answer final mass of unknown mass % water in unknown final mass of unknown Initial observations of your unknown: 28. 201 crucible 12:831 lia 28.2017 Initial mass crucible + lid 41.0329 42.615g Initial mass crucible + lid + unknown 1st heating: mass crucible + lid + unknown 218020 2nd heating: mass crucible + lid + unknown 3rd heating: mass crucible + lid + unknown 41.780g 4th heating: mass crucible + lid + unknown 5th heating: mass crucible + lid +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT