Concatenate, sort and swap array in Java

Posted by sblck on Stack Overflow See other posts from Stack Overflow or by sblck
Published on 2012-09-16T09:16:39Z Indexed on 2012/09/16 9:37 UTC
Read the original article Hit count: 167

Filed under:
|
|
|
|

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();

 }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about sorting