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 3 of 3

Thread: How do I add an index in the middle of an ArrayList?

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question How do I add an index in the middle of an ArrayList?

    Hello,

    I'm having some trouble with adding a specific index from one ArrayList to another. In this case, the index needs to get in the exact middle. I've been trying to solve this with the Math.ceil function, but I get the error:

    'incompatible types: possible lossy conversion from double to int'

    How can I pass this error, and have the code work?

    Thanks!

     public void changeToRow1(Person name)
        {
            if(name.hasVIPpass() == true && maxInRow1<= 8)
            {
                row1.add(Math.ceil(row1.size()/2.0), (name));
                row2.remove(name);
                maxInRow1++;
            }
            else
            {
                System.out.println("Stay in your own row please!");
            }
        }
    Last edited by F_VRBRG; May 2nd, 2022 at 12:10 PM.

  2. #2
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: How do I add an index in the middle of an ArrayList?

    row1.add((int)Math.ceil(row1.size()/2.0), (name));

  3. The Following User Says Thank You to andreaita44 For This Useful Post:

    F_VRBRG (May 2nd, 2022)

  4. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Cool Re: How do I add an index in the middle of an ArrayList?

    Thanks! That was it!

Similar Threads

  1. How to get Median/Middle value in an array?
    By deluxe7 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2014, 03:56 PM
  2. Replies: 6
    Last Post: October 26th, 2013, 01:59 PM
  3. Replies: 3
    Last Post: August 26th, 2013, 04:41 PM
  4. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM
  5. Removing the middle cell
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 7th, 2010, 01:35 PM