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