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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:57:36+00:00 2026-05-14T18:57:36+00:00

I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {

  • 0

I have the following Java code:

import java.util.Arrays;
import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        int[] test = {1,2,3,4,5};
        Collections.rotate(Arrays.asList(test), -1);
        for(int i = 0; i < test.length; i++) { System.out.println(test[i]); }
    }

}

I want the array to be rotated, but the output I get is

1
2
3
4
5

Why is this?

And is there an alternative solution?

EDIT:

So this works:

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

public class Test {
    public static void main(String[] args) {
        int[] test = {1,2,3,4,5};
        List<Integer> testList = new ArrayList<Integer>();
        for(int i = 0; i < test.length; i++) { testList.add(test[i]); }
        Collections.rotate(testList, -1);
        for(int i = 0; i < test.length; i++) { System.out.println(testList.get(i)); }
    }

}

But Arrays.asList is supposed to return a list that when written to, copies the changes to the array. Is there any way to fix this without manually doing the conversion from array to list?

I (think that I) can’t afford to waste that much CPU time and memory to do the conversion.

  • 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-14T18:57:36+00:00Added an answer on May 14, 2026 at 6:57 pm

    This is a tricky problem: yes, asList backs the List it returns with the array, and changes to the List will “write-through” to the array. However, due to how varargs of T... interacts with an array of primitive type in this case, you’re actually creating a list with 1 element!

        int[] test = {1,2,3,4,5};
        System.out.println(Arrays.asList(test).size());
        // prints "1"
    

    Let’s try something different:

        int[] test = {1,2,3,4,5};
        List<Integer> list = Arrays.asList(test);
        // "Type mismatch: cannot convert from List<int[]> to List<Integer>"
    

    As you see, the varargs with an int[] doesn’t work the way you intended, and the compiler gives an error. Arrays.asList actually returns a 1-element List<int[]> instead of a 5-element List<Integer>.

    Using Integer[] instead of int[] works as expected:

        Integer[] test = {1,2,3,4,5};
        Collections.rotate(Arrays.asList(test), -1);
        System.out.println(Arrays.toString(test));
        // prints "[2, 3, 4, 5, 1]"
    

    More explanation

    The full signature of asList is <T> List<T> Arrays.asList(T... a). Note that T can’t be int in this case, for the same reason why you can’t have a List<int> in Java: T needs to be a reference type.

    Consider the following snippet:

        System.out.println(Arrays.asList(1,2,3));
        // prints "[1, 2, 3]"
    

    What happens here is that each int is boxed into an Integer, and the varargs mechanism “works” and asList creates a list of 3 elements. Now consider the following form instead:

        System.out.println(Arrays.asList(new int[] { 1,2,3 }));
        // prints "[[I@xxxxxx]"
    

    Now the argument to asList is an int[]. T can’t be an int, therefore, the T... varargs mechanism “fails”, and asList only gets one element, and it’s an int[], instead of the int values themselves.

    Now consider this form:

        System.out.println(Arrays.asList(new Integer[] { 1,2,3 }));
        // prints "[1, 2, 3]"
    

    Now since Integer[] is a T..., asList gets 3 elements as expected.

    See also

    • Java: Array of primitive data types does not autobox
      • An int[] does not autobox to an Integer[]
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 448k
  • Answers 448k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's a few approaches you could take. The one you… May 15, 2026 at 7:52 pm
  • Editorial Team
    Editorial Team added an answer Instead of re-indexing every minute try using the Delayed Deltas… May 15, 2026 at 7:52 pm
  • Editorial Team
    Editorial Team added an answer I think the SortedSet<T> class in System.Collections.Generic is what you're… May 15, 2026 at 7:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.