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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:41:08+00:00 2026-06-11T08:41:08+00:00

I am trying to concatenate two arrays into new array, sort in order, and

  • 0

I am trying to concatenate two arrays into new array, sort in order, and swap two values of index.

I’m kind of new to java and only use C before so having a hard time handling an Object.

In main method it declares two object arrays
IntVector vector = new IntVector(3); and IntVector vector2 = new IntVector(3);

I can only do this if the types are int[], but I want to use as an object
How should I code the concat, sort, and swap method?

public class IntVector {

private int[] items_;
private int itemCount_;

private IntVector(int[] data, int n) {
      items_ = data.clone();
      itemCount_ = n;
    }


 public IntVector(int itemSize)
 {
  itemCount_ =0;

  if(itemSize<1) itemSize =10;

  items_ = new int[itemSize];
 }

 public void push(int value)
 {
  if(itemCount_ + 1 >= items_.length)
   overflow();

  items_[itemCount_++] = value;
 }

 public void log()
 {
  for (int i=0 ; i<itemCount_; ++i)
  {
   System.out.print(items_[i]);

   if(i<itemCount_ -1)
    System.out.println();
  }
 }

 public void overflow()
 {
  int[] newItems = new int[items_.length * 2];

  for(int i=0 ; i<itemCount_; ++i)
  {
   newItems[i] = items_[i];
  }
  items_=newItems;
  }

 public int getValue(int index)
 {
  if(index < 0 || index >= itemCount_)
  {
   System.out.println("[error][IntVector][setValue] Incorrect index=" + index);
   return 0;
  }
  return items_[index];
 }

 public void setValue(int index, int value)
 {
  if(index < 0 || index >= itemCount_)
  {
   System.out.println("[error][IntVector][setValue] Incorrect index=" + index);
   return ;
  }
  items_[index] = value;
 }


public IntVector clone()
 {

    return new IntVector(items_, itemCount_);
 }

public IntVector concat()
 {

    return null;
 }

public IntVector sort()
 {

    return null;
 }

public IntVector swap()
 {

    return null;
 }



 public static void main(String[] args)
  {
   IntVector vector = new IntVector(3);
   IntVector vector2 = new IntVector(3);

   vector.push(8);
   vector.push(200);
   vector.push(3);
   vector.push(41);

   IntVector cloneVector = vector.clone();

   vector2.push(110);
   vector2.push(12);
   vector2.push(7);
   vector2.push(141);
   vector2.push(-32);

   IntVector concatResult = vector.concat(vector2);
   IntVector sortResult = concatResult.sort();
   IntVector swapResult = sortResult.clone();


   //swapResult.swap(1,5);

   System.out.print("vector : "); vector.log();
   System.out.print("\n\ncloneVector : "); cloneVector.log();
   System.out.print("\n\nvector2 : "); vector2.log();
   System.out.print("\n\nconcatvector : "); concatResult.log();
   System.out.print("vector : "); vector.log();
   System.out.print("vector : "); vector.log();

 }

}

  • 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-11T08:41:09+00:00Added an answer on June 11, 2026 at 8:41 am

    The Apache Commons library has an ArrayUtils which you can use to concatenate arrays.

    To sort the arrays you can use Arrays.sort() which is part of the standard SDK. There is one with a comparator if you want to custom sort

    To swap your method takes no parameters so I am not sure what element you want to swap. If you want to elements in two indicies you could do

    private swap (int pos1, int pos2) {
        //do some checking to ensure pos1 and pos2 exist here...
        int tmp = items_[pos1];
        items_[pos1] = items_[pos2];
        items_[pos2] = tmp;
    }
    

    But as everyone has said I would also recommend using the Collections framework. It will make the concatenation a lot simpler. And there is a dedicated swap method

    Edit

    I have just found how-to-concatenate-two-arrays-in-java which may help with the concatenation.

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

Sidebar

Related Questions

I'm trying to concatenate two properties into one string like so: public class thing
I am trying to concatenate two date columns, putting a - between the values.
What I'm trying to do is concatenate two fields of a table, sort them
Im trying to extract two (int32_t) values, and place them within a char array.
I am trying to use the addObjectsFromArray to concatenate (so to speak) two arrays
I am trying to concatenate two video files with the gnonlin components of the
I am trying to concatenate two strings with NSString initWithFormat, and I am getting
I am trying to concatenate two fields from a list to display in a
I am wanting to concatenate two nodeset values using XPath in XForms. I know
I'm trying to concatenate two uint32_t and get back a uint64_t. Here's my method

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.