0

//I need to ask the user for the size of the list they want, then generate random numbers with an array list for the size list they want, and output the list. These steps I have done but then I have to output the number of odd numbers in the list using an enhanced for loop and then reprint the list without even numbers.//

import java.util.Scanner;
import java.util.ArrayList;

public class MyProgram
{
public static void main(String[] args)
{
  Scanner scan = new Scanner(System.in);
  System.out.println("How big would you like your list?");
  int decision = scan.nextInt();
  
  
  
  ArrayList<Integer> Rando = new ArrayList<Integer>();
  for (int i = 1; i <= decision; i++)
  {
      Rando.add((int)(Math.random()*100 + 1));
  }
  System.out.println();
  System.out.println(Rando);
  
  
  
  ArrayList<Integer> evens = Rando;
  for (int i = 0; i < evens.size(); i++)
  {      
    if (evens.get(i)%2 != 0) 
    {
        evens.remove(i);
       
    }    
  }
   
    
ArrayList<Integer> odds = Rando;
  for (int i = 0; i < odds.size(); i++)
  {      
    if (odds.get(i)%2 != 0) 
    {
        evens.remove(i);
    }    
  }
  System.out.println();
  System.out.println(odds);
  
  for(Integer number: Rando)
  {
      System.out.println();
      System.out.println("# of Odd Numbers: "+ odds.size());
      System.out.println();
      break;
  }
 
  System.out.println("List Without Evens: " + evens.remove);
  System.out.println();
  
  
}
  
}
  • 1
    when you are doing `ArrayList evens = Rando;` you need to do `ArrayList evens = new ArrayList(Rando);`, and same with `odds`. the problem is that if you do `list1 = list2` then these list are the same object, if you change one, you change the other. for more information look on https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Ofek Jan 26 '22 at 15:35
  • Do you not know the syntax for the enhanced [for](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) loop? `for (Integer number : evens) {` – Abra Jan 26 '22 at 15:38
  • Are you allowed to do everything in a single loop? It would be easier. – WJS Jan 26 '22 at 16:55

1 Answers1

0

The way you are doing it now you won't have anything left in the list.

ArrayList<Integer> evens = Rando;
... 
ArrayList<Integer> odds = Rando;

This will modify the original list. You won't have anything left in the list after the first two loops, which remove the even numbers and then the odd. I think instead you should create a new list.

  ArrayList<Integer> evens = new ArrayList<>();
  for (int i = 0; i < Rando.size(); i++)
  {      
    if (Rando.get(i)%2 == 0) 
    {
        evens.add(Rando.get(i));
       
    }    
  }

Now using the for-each loop becomes easier.

  ArrayList<Integer> evens = new ArrayList<>();
  for (int x : Rando )
  {      
    if (x%2 == 0) 
    {
        evens.add(x);
       
    }    
  }

Remember you need to modify the loops you have now. Rando will contain no elements at all the way you have it, so you have to change the existing code or re-create the random number list each time.

markspace
  • 10,621
  • 3
  • 25
  • 39