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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:43:23+00:00 2026-05-11T03:43:23+00:00

I wanted to write a function that would take an object and convert it

  • 0

I wanted to write a function that would take an object and convert it to an array that contains that object as a single element. It occurred to me that I could maybe do this with generics and variable arguments to essentially do this automatically, without the need to write a function for each object type I wished to use. Will this code work? Are there any subtleties I need to be aware of?

public static <X> X[] convert_to_array(X... in_objs){     return in_objs; } 
  • 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. 2026-05-11T03:43:23+00:00Added an answer on May 11, 2026 at 3:43 am

    It works but it seems like:

     Object o = new Object();  someMethod(new Object[] { o } ); 

    is a little more straightforward then:

    Object o = new Object(); someMethod(convert_to_array(o)); 

    In cases where sometimes I want to pass a single object, but other times I want to pass an array, usually I just use an overloaded method in the API:

    public void doSomething(Object o) {     doSomething(new Object[] { o } ); }  public void doSomething(Object[] array) {     // stuff goes here. } 

    Varargs can be used but only if the array is the last parameter of course.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Put the information that people using the functions need to… May 16, 2026 at 12:42 pm
  • Editorial Team
    Editorial Team added an answer The request object is a Webob request object. As such,… May 16, 2026 at 12:42 pm
  • Editorial Team
    Editorial Team added an answer The problem of course, is that BitMap (like many .Net… May 16, 2026 at 12:42 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

Related Questions

I'm playing around with beginner Haskell, and I wanted to write an average function.
I made a Python function to convert dictionaries to formatted strings. My goal was
Recently I was talking to a co-worker about C++ and lamented that there was
I have a faulty hard drive that works intermittently. After cold booting, I can
I have a bad feeling that the answer to this question is no, but
Assume that i have written a program in C++ without using RTTI and run-time
I'm new to Clojure and have been using Compojure to write a basic web
I was wondering which IDE choice you flash developer use to write the code....I
I've got an extended WebBrowser control which handles the NewWindow3 event. Originally I wanted
From a response to some question yesterday, I learned that it is nonportable and

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.