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

 }

}

sblck
  • 137
  • 1
  • 6
  • 14
  • I can only code in this way when the types are array of int public int[] concat2(int[] A, int[] B) { int[] concat_ary = new int[10]; for(int i=0 ; i=0; i--) { for(int j=0; jA[j+1]) { int temp =A[j]; A[j]=A[j+1]; A[j+1] =temp; } } } } } – sblck Sep 16 '12 at 09:17
  • 3
    Any reason why you are not using the Collections framework for this? – Maarten Bodewes Sep 16 '12 at 09:21
  • 2
    As owlstead suggested, I'd take a look at the [Collections Tail](http://docs.oracle.com/javase/tutorial/collections/index.html) – MadProgrammer Sep 16 '12 at 09:27
  • 1
    I'm guessing that this is an assignment of some kind, otherwise as everyone is suggesting just use Collections rather than re-implementing. If it is an assignment you're wanting help with it would be good to be clear about the constraints you are under, e.g. core java library only? – EdC Sep 16 '12 at 09:59
  • @owlstead fat fingers + iPad = bad idea :P ... Shame I can't fix it – MadProgrammer Sep 16 '12 at 10:55
  • @MadProgrammer yeah, had the same thing with my mobile phone a few times – Maarten Bodewes Sep 16 '12 at 10:56

2 Answers2

2

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.

Community
  • 1
  • 1
RNJ
  • 15,272
  • 18
  • 86
  • 131
1

If you really want to do all of this in a typical Java way, you'd need the following:

a) Create two int arrays initialized with necessary values.

b) Convert the two arrays into List using the Arrays utility class. See the asList() method.

c) Create an ArrayList and add the above two lists to it. See the addAll() method.

d) Sort this List based on the natural order. See the Collections.sort() method or create your own Comparator and apply the sorting.

e) Convert the List to an int array using the toArray(T[] t)method.

The last int array you got should be the end result.

As @owlstead commented, using the Java Collections API would be lot simpler than going low level manipulating arrays yourself.

asgs
  • 3,928
  • 6
  • 39
  • 54