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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:01:56+00:00 2026-05-16T12:01:56+00:00

In some code I’m creating a List of Bytes, and want to insert an

  • 0

In some code I’m creating a List of Bytes, and want to insert an array of bytes into the list as I am building it. What is the cleanest way of doing this? See code below – thanks.

public class ListInsert {
    public static byte[] getData() {
        return new byte[]{0x01, 0x02, 0x03};
    }

    public static void main(String[] args) {
        final List<Byte> list = new ArrayList<Byte>();
        list.add((byte)0xaa);
        list.add(getData()); // I want to insert an array of bytes into the list here
        list.add((byte)0x55);
    }
}
  • 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-16T12:01:57+00:00Added an answer on May 16, 2026 at 12:01 pm

    IF you have a Byte[] arr — an array of reference types — you can use Arrays.asList(arr) to get a List<Byte>.

    IF you have a byte[] arr — an array of primitives — you can’t use Arrays.asList(arr) to get a List<Byte>. Instead you’ll get a one-element List<byte[]>.

    That is, while a byte can be boxed to a Byte, a byte[] DOES NOT get autoboxed to Byte[]!
    (also true for other primitives)

    So you have two choices:

    • Just iterate over each byte in byte[] and add individually
    • Use libraries
      • With Apache Commons Lang, you can convert byte[] to Byte[]
        • You can then Arrays.asList and addAll
      • With Guava can convert byte[] immediatelly to List<Byte>

    The first option looks like this:

    byte[] arr = ...;
    for (byte b : arr) {
        list.add(b);
    }
    

    The second option with Guava looks like this:

    // requires Guava
    byte[] arr = ...;
    list.addAll(Bytes.asList(arr));
    

    This uses Bytes.asList from package com.google.common.primitives. The package has other conversion utilities for other primitives too. The entire library is highly useful.

    With Apache Commons Lang, you can use Byte[] toObject(byte[]) from ArrayUtils:

    // requires Apache Commons Lang
    byte[] arr = ...;
    list.addAll(Arrays.asList(ArrayUtils.toObject(arr)));
    

    Related questions

    • How to convert int[] into List<Integer> in Java?
    • Arrays.asList() not working as it should?
    • Performance impact of autoboxing
    • Most useful free third party Java libraries?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code like this in a winforms app I was writing to
Here is some code I could not get to format properly in markdown, this
I have added some code which compiles cleanly and have just received this Windows
Some code for context: class WordTable { public: WordTable(); ~WordTable(); List* GetListByAlphaKey(char key); void
some code snippets. The java coding doing the jaxb unmarshaling. pretty straightforward, copied out
In some code I've inherited, I see frequent use of size_t with the std
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I'm maintaining some code that uses a *= operator in a query to a
Basically I have some code to check a specific directory to see if an
I have written some code in my VB.NET application to send an HTML e-mail

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.