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 8742167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:27:05+00:00 2026-06-13T11:27:05+00:00

how can I fill the two lists with corresponding elements in method fillArrayList the

  • 0

how can I fill the two lists with corresponding elements in method fillArrayList the user must enter the values

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

class Assignment1
{
    public static void main(String[] args)
    {
      ArrayList<Integer> list1 = new ArrayList<Integer>();
      ArrayList<Integer> list2 = new ArrayList<Integer>();

      System.out.println("Enter your lists = ");
      fillArrayList(list1,list2);

    }

    public static void fillArrayList(ArrayList<Integer> L1 ,ArrayList<Integer> L2 )
    {
           //don't know what to do here to fill two lists from the user , really need help

    }
  • 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-13T11:27:06+00:00Added an answer on June 13, 2026 at 11:27 am

    I think you should modify your fillArrayList signature to just fill one list at a time. And you can invoke it twice for your two arraylist.

    So, your method should be like: –

    public static void fillArrayList(ArrayList<Integer> myList) {
        /* Let the code be same as the one in @AmitD's post */
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter elemetns for List: Enter -1 to end ");
    
        while (scanner.hasNext()) {
            if (scanner.hasNextInt()) {
                int i = scanner.nextInt();
                if (i == -1)
                    break;
    
                myList.add(i);
            } else {
                System.out.println("Invalid value Please enter integer");
                scanner.next();
            }
        }
    
    }
    

    Then declare your two lists as instance variable, outside the main method. And use List for reference type.

    List<Integer> list1 = new ArrayList<Integer>();
    List<Integer> list2 = new ArrayList<Integer>();
    

    Now, from the main method, you can invoke this method twice for each list like this: –

    fillArrayList(list1);
    fillArrayList(list2);
    

    And back in your main method, you can print your list, as the change you do in fillArrayList method will get reflected there also.

    System.out.println(list1);
    System.out.println(list2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a HTML layout where user can fill the data and save
I am trying to make the situation like this: user can either fill in
Is there an image processing algorithm that can fill these gaps within an image?
I have a from where you can fill in any combination of your home,
In Short: I have a PDF that customers can fill out. When they press
I know in C++ and in PHP you can fill a string or a
I have an ecommerce gift store where users can fill out a gift-card for
I have a table with a form below it. You can fill out the
I'm a pretty new C# programmer. I was wondering if someone can fill me
On a CAShapeLayer , I've drawn a closed UIBezierPath . I can fill this

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.