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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:59:48+00:00 2026-06-11T19:59:48+00:00

I try to generate java code with SWIG In MyList.h I declared a custom

  • 0

I try to generate java code with SWIG

In MyList.h I declared a custom list object called _list

List<T*> _list;

and this List class inherits from vector

class List : public vector<T>

In a business class (in C++) I return a List of custom objects

List<MyObject> getMyList(){
   ....
   return list;
}

so I want to generate java code where I can retrieve this C++ List as java.util.List or java.util.Vector.

in my swig.i file I couldn’t manage how to embody

%typemap(jstype) List "java.util.Vector"
namespace std {
   %template(CustomVector) vector<MyObject>;
}

any kind help how to configure this swig.i template file or some sample code to generate a java.util.List / Vector returning function will be appreciated.

Thank you.

  • 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-11T19:59:49+00:00Added an answer on June 11, 2026 at 7:59 pm

    You don’t really want to be touching java.util.Vector with your wrapped interfaces because you will end up duplicating storage or making large numbers of copy operations every time you pass it in/out of a function. (Note also that in general in C++ inhering from containers is odd design).

    Instead in Java the “right” thing to do is to inherit from java.util.AbstractList. This answer is a more generic version of my older answer to a similar question.

    It works for all std::vector types, not just a fixed type and handles primitives which need to be accessed via Objects using an “autobox” custom type map. It’s missing support for the specialized std::vector<bool>, but that should be simple to add if you need it.

    %{
    #include <vector>
    #include <stdexcept>
    %}
    
    %include <stdint.i>
    %include <std_except.i>
    
    namespace std {
    
        template<class T> class vector {
          public:
            typedef size_t size_type;
            typedef T value_type;
            typedef const value_type& const_reference;
            vector();
            vector(size_type n);
            vector(const vector& o);
            size_type capacity() const;
            void reserve(size_type n);
            %rename(isEmpty) empty;
            bool empty() const;
            void clear();
            void push_back(const value_type& x);
            %extend {
                const_reference get(int i) const throw (std::out_of_range) {
                    return $self->at(i);
                }
                value_type set(int i, const value_type& VECTOR_VALUE_IN) throw (std::out_of_range) {
                    const T old = $self->at(i);
                    $self->at(i) = VECTOR_VALUE_IN;
                    return old;
                }
                int32_t size() const {
                  return $self->size();
                }
                void removeRange(int32_t from, int32_t to) {
                  $self->erase($self->begin()+from, $self->begin()+to);
                }
            }
        };
    }
    
    // Java typemaps for autoboxing in return types of generics
    %define AUTOBOX(CTYPE, JTYPE)
    %typemap(autobox) CTYPE, const CTYPE&, CTYPE& "JTYPE"
    %enddef
    AUTOBOX(double, Double)
    AUTOBOX(float, Float)
    AUTOBOX(boolean, Boolean)
    AUTOBOX(signed char, Byte)
    AUTOBOX(short, Short)
    AUTOBOX(int, Integer)
    AUTOBOX(long, Long)
    AUTOBOX(SWIGTYPE, $typemap(jstype,$1_basetype))
    
    %typemap(javabase) std::vector "java.util.AbstractList<$typemap(autobox,$1_basetype::value_type)>"
    %typemap(javainterface) std::vector "java.util.RandomAccess"
    %typemap(jstype) std::vector get "$typemap(autobox,$1_basetype)"
    %typemap(jstype) std::vector set "$typemap(autobox,$1_basetype)"
    %typemap(jstype) std::vector &VECTOR_VALUE_IN "$typemap(autobox,$1_basetype)"
    %typemap(javacode) std::vector %{
      $javaclassname(java.util.Collection<$typemap(autobox,$1_basetype::value_type)> e) {
        this.reserve(e.size());
        for($typemap(autobox,$1_basetype::value_type) value: e) {
          this.push_back(value);
        }
      }
    %}
    

    Most of this is fairly similar to the default std_vector.i that SWIG currently provides, the new bits are the renaming, extending and typemaps that extend AbstractList and implement RandomAccess. It also adds a constructor that takes other Collections – this is recommended by the Java documentation and easy enough to do. (There’s an overload for other std::vector types which is much faster).

    I tested this vector wrapping within another SWIG interface:

    %module test
    
    %include "vector.i"
    
    %template(DblVec) std::vector<double>;
    %template(ByteVec) std::vector<signed char>;
    %include <std_string.i>
    %template(StringVec) std::vector<std::string>;
    
    %inline %{
    struct foo {};
    %}
    
    %template(FooVec) std::vector<foo>;
    

    Which I was able to compile and run with:

    public class run {
      public static void main(String argv[]) {
        System.loadLibrary("test");
        DblVec dv = new DblVec(100);
        for (int i = 0; i < 100; ++i) {
          dv.set(i,(double)i);
        }
        FooVec fv = new FooVec(1);
        fv.set(0, new foo());
        for (double d: dv) {
          System.out.println(d);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
I try to debug a javascript function called from Generate Thumbnails plugin in Wordpress.
I have this code try { //AN EXCEPTION IS GENERATED HERE!!! } catch {
I am facing an issue with SecureRandom in java. This was the code that
I am trying to generate java code using jcodemodel in my maven project. My
I try to generate some Excel sheets in Java application using JExcelAPI (v. 2.6.3)
We're using iText to generate PDF files from Java code, which works pretty well
This code is used to generate a XML document from its String representation. It
The following two code generate different result: def x = try{ true } finally
I'm using SWIG to export a c++ class to Java, but ran into a

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.