Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:08:34+00:00 2026-06-14T14:08:34+00:00

So here is my program: Create an ArrayList that will only contain strings Add

  • 0

So here is my program:

Create an ArrayList that will only contain strings
Add the following to the list in order

  • Mary
  • John
  • Mahendra
  • Sara
  • Jose
  • Judy

Print the list using the enhanced for loop
Insert Harry in front of Mahendra and after John
Then Remove position 4 from the list

Here’s what I’ve written:

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

public class Name {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<String>();
        Scanner input = new Scanner(System.in);
        names.add(input.nextLine());
        names.add(input.nextLine());
        names.add(input.nextLine());
        names.add(input.nextLine());
        names.add(input.nextLine());
        names.add(input.nextLine());
        names.add(input.nextLine());

        for (String n : names) {
        System.out.println(n);
        }
    }
}

I guess I’m having problems with adding and removing. I believe everything else should be fine though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:08:35+00:00Added an answer on June 14, 2026 at 2:08 pm

    You may want to use below methods to insert and remove:

    void    add(int index, E element)
     E      remove(int index)
    

    e.g. Mahendra is at index 2(index starts from 0), then to add Harry in front of Mahendra, just do as below:

      names.add(2, "Harry"); //This will push Mahendra at index 3
    

    To remove crrent index 4,

      names.remove(4);
    

    To remove previous index 4, which has become index 5 now,

      names.remove(5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a simple sorting program of an ArrayList: ArrayList<String> list = new ArrayList<String>();
there is this check-in-out program here at my workplace, it only takes the data
OK, here is a program that prompts a user for basketball player input. Well
I have a program that gets a remote xml file and creates a list
Here is my code currently. I'm making a java program that seaches Active Directory
I made an ATM program. I have a try catch that will ask for
I have a class Line that contains an internal ArrayList<Double> . In my program,
Got a simple Javascript program here to accept and check a password. It should:
I'm trying to build a hello-world program here to check my android kit installation.
I am trying to send mail using SmtpClient() within my Winforms VB.Net program Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.