Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Assistance with Arraylist

  1. #1
    Junior Member
    Join Date
    Nov 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Assistance with Arraylist

    want to list the arrays that are even within this method. Instead of printing the actual elements, I'm printing the index that the element is in. What am I doing wrong?



    public static void evenNumbers(ArrayList<Integer> temp) {

    ArrayList<Integer> count = new ArrayList<Integer>();

    for (int i = 0; i < temp.size(); i++) {

    if (temp.get(i) % 2 == 0)

    count.add(i);

    }

    for (int i = 0; i < count.size(); i++) {



    System.out.println("The even numbers of Temperatures is: " +count.get(i));



    }

    }

    //count.get(i) && count array list prints the index instead of the element inside the index.

    //thanks everyone.

  2. #2
    Junior Member
    Join Date
    Nov 2022
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Assistance with Arraylist

    I've solved the issue.



    public static void evenNumbers(ArrayList<Integer> temp) {

    ArrayList<Integer> count = new ArrayList<Integer>();

    // int n = 0;

    for (int i = 0; i < temp.size(); i++) {

    if (temp.get(i) % 2 == 0) {

    count.add(i,temp.get(i)); // <----- the solution.

    }

    }



    //Thanks Everyone

Similar Threads

  1. Need any assistance
    By Chin24 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 16th, 2018, 01:21 PM
  2. [SOLVED] ArrayList assistance
    By mrivera85 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 2nd, 2014, 08:48 PM
  3. In need of assistance!
    By Nexcit in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 11th, 2014, 05:24 AM
  4. Need some assistance please
    By JavaPhish in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 10:00 AM
  5. New to the complmunity, need some assistance please.
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 7th, 2011, 06:14 AM

Tags for this Thread