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

  • Home
  • SEARCH
  • 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 4103692
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:59:28+00:00 2026-05-20T20:59:28+00:00

Hi Here i am trying to sublist the items from list and print them

  • 0

Hi Here i am trying to sublist the items from list and print them 5 for each iteration.
here in the following code it is printing same items every time

for(int i=0;i<4;i++)
    {
    List<Card> l=a.subList(a.size()-5, a.size());
    System.out.println(l);

 }

But here it is printing different items as if it is removing 5 from the list each time

 for(int i=0;i<4;i++){
     int deckSize = a.size();
     List<Card> handView = a.subList(deckSize-5, deckSize);
     ArrayList<Card> hand = new ArrayList<Card>(handView);
     handView.clear();
     System.out.println(hand);
 }

what is the difference between the above two code snippets

  • 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-05-20T20:59:28+00:00Added an answer on May 20, 2026 at 8:59 pm

    You should have a read at the API for List.

    The returned list is backed by this
    list, so non-structural changes in the
    returned list are reflected in this
    list, and vice-versa.

    So in each case the list you are creating is not a new copy of the elements from the original list, but just a view into the original list. In the second example you are calling clear on the new list, which is actually clearing those elements in the original list, hence the behaviour you are seeing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm hitting a wall here while trying to access items from Magento on an
in post: Copy sublist from list was stayed explained me that for copy a
This is a lot of code but I'm at ends here trying to figure
Here am trying to get values from a mysql table with the name lead.
Newb here trying to fix my php code. Getting an error at line 89.
I've a problem here trying to run a function from a a href placed
I am trying to get value from an HTML list <div id=test> <ul> <li>C:\
I'm checking datapicker documentation here trying to find a way to change it from
Beginner here trying to get a pipeline working in bash. If somebody can see
I am going nuts here trying to resolve a cascading update/delete issue :-) I

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.