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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:06:13+00:00 2026-06-13T13:06:13+00:00

why when I call ArrayList<String> reversedList = ListUtils.reverse(list); is modifying the original list, even

  • 0

why when I call

ArrayList<String> reversedList = ListUtils.reverse(list);

is modifying the original list, even though I’m making a copy of it inside the method

package com.javablackbelt.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ListUtils {

    public static void main(String arg[]) {
        String[] arr = {"one", "two", "three", "four", "five"};
        ArrayUtils.print(arr);

        System.out.println();

        String [] reversedArr = ArrayUtils.reverse(arr);
        ArrayUtils.print(reversedArr);

        System.out.println();

        ArrayList<String> list = ArrayUtils.toArrayList(arr);
        ListUtils.print(list);  

        ArrayList<String> reversedList = ListUtils.reverse(list);
        ListUtils.print(reversedList);

        String[] arrFromList = ListUtils.toArray(list);
        ArrayUtils.print(arrFromList);

    }

    public static void print(ArrayList<String> aStr) {
        System.out.print("list: [ ");
        for(String l: aStr)
            System.out.print(l+" ");
        System.out.println(" ] size: "+aStr.size());
    }

    public static ArrayList<String> reverse(ArrayList<String> aList) {
        ArrayList<String> newList = aList;

        Collections.reverse(newList);
        return newList;
    }

    public static String[] toArray(ArrayList<String> list) {
        ArrayList<String> newList = list;
        String[] newStr = newList.toArray(new String[list.size()]);

        return newStr;
    }

}
  • 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-13T13:06:14+00:00Added an answer on June 13, 2026 at 1:06 pm

    ArrayList<String> newList = aList; is not creating a new list. It creates a new reference to the same list.

    Creating a new list could be done this way:

    ArrayList<String> newList = new ArrayList<String>(aList.size());
    for (String str : aList)
        newList.add(str);
    

    Thx @JB Nizet
    This is much simpler:

    ArrayList<String> newList = new ArrayList<String>(aList);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method like this- public List<Apples> returnFruits(String arg1){ List<Apples> fruits = new
I have an ArrayList of Strings, size of which may vary. List<String> strList1 =
I have an ArrayList<String> that I'd like to return a copy of. ArrayList has
List l = new ArrayList<Number>(); List<String> ls = l; // unchecked warning l.add(0, new
Regarding : List<String> mystring = new ArrayList<String>(); Correct me if Im wrong: List is
This API call returns a potentially large List<String>, which is not sorted. I need
If I have two class constants: List<String> workingList= new ArrayList<String>(); StringBuilder holder = new
I have an arraylist of objects in my servlet where I want to call
Call to undefined method after upgrading to PHP 5.4.0 mentions a few APC bug-reports
When I execute following code in scala REPL console: java.util.Collections.max(new java.util.ArrayList[String]()) NoSuchMethodError exception is

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.