QuickSort trouble java.lang.ArrayIndexOutOfBoundsException: 10 - java

I have some problems with java.lang.ArrayIndexOutOfBoundsException: 10,
if i set 1 instead of 0 - i will have sorted array with unsorted first element, if i set 0 - i have error
public void quicksort() {
// Recursion
quicksort(0, counter - 1);
}
Here is all my code
public class Main {
private static int comparations = 0;
private static int swaps = 0;
int[] array;
int[] a;
int counter = 0;
int size;
public void qwe() throws IOException {
Scanner scan = new Scanner(new File("input.txt")); //provide file name from outside
while(scan.hasNextInt())
{
counter++;
scan.nextInt();
}
System.out.println(counter);
Scanner scan2 = new Scanner(new File("input.txt"));
a = new int[counter];
for(int i=0;i<counter;i++)
{
a[i]=scan2.nextInt(); //fill the array with the integers
}
}
public int partition(int p, int q) {
int i = p;
int j = q + 1;
// Get the pivot element from the middle of the list
int pivot = a[p];
// Divide into two lists
do {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
do {
i++;// As we not get we can increase i
} while (a[i] < pivot);
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
do {
j--;// As we not get we can increase j
} while (a[j] > pivot);
// If we have found a values in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
if (i < j) {
swap(i, j);
}
} while (i < j);
// swap the pivot element and j th element
swap(p, j);
return j;
}
private void swap(int p, int j) {
// exchange the elements
int temp = a[p];
a[p] = a[j];
a[j] = temp;
swaps++;
}
public void quicksort() {
// Recursion
quicksort(0, counter - 1);
}
public void quicksort(int p, int q) {
int j;
if (p < q) {
// Divide into two lists
j = partition(p, q);
// Recursion
quicksort(p, j - 1);
quicksort(j + 1, q);
}
comparations++;
}
public void print() {
// print the elements of array
for (int i = 0; i < counter; i++) {
System.out.print(a[i] + ",");
}
System.out.println();
}
public static void main(String args[]) throws IOException {
Main q = new Main();
q.qwe();
System.out.println("Before Sort <<<<<<<<<<<<<<<<<<<<<");
q.print();
q.quicksort();
System.out.println("After Sort > > > > > > > > > > > >");
q.print();
System.out.println("Comparisons: " + comparations);
System.out.println("Swaps: " + swaps);
}
}

use the condition in partition method
while(a[i] < pivot && i<q)
instead of
while(a[i] < pivot)
because you have to stop searching bigger value than pivot when you reach at the the end

I think you have to avoid do{...} while and use while instead.
Something like:
public int partition(int p, int q) {
int i = p;
int j = q + 1;
// Get the pivot element from the middle of the list
int pivot = a[p];
// Divide into two lists
while (i < j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (a[i] < pivot) {
i++;// As we not get we can increase i
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (a[j] > pivot) {
j--;// As we not get we can increase j
}
// If we have found a values in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
if (i < j) {
swap(i, j);
}
}
// swap the pivot element and j th element
swap(p, j);
return j;
}

I suspect your partition code isn't correct. As swap should be done on basis of value not on index.
if (i < j) {
swap(i, j);
}
Partitioning: reorder the array so that all elements with values
less than the pivot come before the pivot, while all elements with
values greater than the pivot come after it (equal values can go
either way). After this partitioning, the pivot is in its final
position. This is called the partition operation.
Also, why are you reading same file twice can't you get the number of elements and elements in same loop ?

Related

Recursive Selection Sort in Java

I need to implement this algorithm creating a new ArrayList object at each recursive call.
My starting Array contains integers in this order"20, 40 ,10, 30 ,50 ,5" , after sorting it I have 5,5,5,5,5,5. I think that the problem is in the recursive call and in the last for cicle of the SelectionSort because removing the last for I notice that the the first element is sorted correctly.
import java.util.*;
public class SelectionSort {
//var
public ArrayList<Integer> arr ;
//constructor
public SelectionSort(ArrayList<Integer> arr){
this.arr = arr;
}
public ArrayList<Integer> getarraylist() {
return arr;
}
public void sort(){
//position of the last sorted element
int minimum =0;
if (arr.size() <=0 ) return;
for ( int j = 1; j < arr.size()-1; j++ ) {
if (arr.get(j) < arr.get(0) ) {
minimum = j;
//swap element 0 and minimum
int temp = arr.get(0);
arr.set(0, arr.get(minimum));
arr.set(minimum, temp);
}
}
//recursive call, new array without first element (already sorted)
ArrayList<Integer> arr2 = new ArrayList<>(arr.subList(1,arr.size()));
SelectionSort s2 = new SelectionSort(arr2);
s2.sort();
for(int i=0;i<s2.getarraylist().size();i++) {
arr.set(i, s2.getarraylist().get(i));
}
}
Driver class
public class Main {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<Integer (Arrays.asList(20,40,10,30,50,5));
System.out.println("\n ARRAY ELEMENTS \n ");
for (int i: arr) {
System.out.println(i);
}
System.out.println("\n SORTED ELEMENTS \n ");
SelectionSort s = new SelectionSort(arr);
s.sort();
for (int i: s.getarraylist()) {
System.out.println(i);
}
}
}
You have actually two bugs in your algorithm that, together, lead to the observed output.
The first bug is within the for-loop that determines the minimal element:
for ( int j = 1; j < arr.size()-1; j++ ) { ...
You terminate one element too early, i.e. the last element is never considered. Thus, after the first iteration, the 5 is the last element in your ArrayList. In fact, it is the last element in every of your ArrayLists. The fix is to not subtract 1 in the for-condition:
for ( int j = 1; j < arr.size(); j++ ) { ...
The second bug is in your last for-loop where you copy the values from index i of s2 to index i of arr. You neglect the fact that s2 is one element shorter than arr. Thus, the only element not overriden is the last element. The fix is to get the i-th element from s2, but write it at the i + 1-th index of arr:
arr.set(i + 1, s2.getarraylist().get(i));
Now let us take look at how those two bugs lead to the observed output. Since
the last element in your ArrayList is never overridden and
the last element is always the same,
all elements have the same value (in your test case: 5).
Some remarks on your code:
the variable minimum is superfluous and can be replaced with j.
If you replace all occurences of ArrayList in SelectionSort with List, you can actually simplify the last part of your code to:
// remove the line declaring arr2, it is no longer needed
SelectionSort s2 = new SelectionSort(arr.subList(1, arr.size()));
s2.sort();
// last for-loop not needed anymore, method ends here
This is possible because ArrayList#subList(...) states that "The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa."
You should take a little bit more care wrt. indentation.
You have some minor inconsistencies in your coding style. For example, sometimes you write a blank after ( or before a ) or {, sometimes you do not. See what you like more and use it uniformly.
In Java, variable-, parameter- and methodnames should be written in camelCase (getarraylist() -> getArrayList())
With your last loop:
for(int i=0;i<s2.getarraylist().size();i++) {
arr.set(i, s2.getarraylist().get(i));
}
This overrides every element with the same number. (Why you got all 5's as your result) This is because you only iterate to the second to last element (arr.size()-1). Then you copy the elements in the line:
ArrayList<Integer> arr2 = new ArrayList<>(arr.subList(1,arr.size()));
Eventually you are only copying the last element over (5) and then copying this to the final ArrayList arr.
Also you create another SelectionSort object every time you call the sort method. This is not good.
Here is the code I wrote:
public void sort(List<Integer> list){
//position of the last ordered element
int minimum =0;
if (list.size() <=0 ) return;
for ( int j = 1; j < list.size(); j++ ) {
if (list.get(j) < list.get(0) ) {
minimum = j;
//swap element 0 and minimum
int temp = list.get(0);
list.set(0, list.get(minimum));
list.set(minimum, temp);
}
}
sort(list.subList(1,list.size()));
}
I changed it to accept an argument of List<Integer> (Because the subList() method returns a List) and then got rid of the last loop and where you created new objects.
Also youll have to change
s.sort();
to:
s.sort(s.getarraylist());
Output:
ARRAY ELEMENTS
20
40
10
30
50
5
SORTED ELEMENTS
5
10
20
30
40
50
Im not sure I understand the question, but I created a recursive (and iterative) selectionSort and InsertionSort just for fun, hope it helps.
public class Sorts {
public static void swap(Comparable[] a, int i, int j) {
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void selectionSortItr(Comparable[] a, int n) {
for (int i = 0; i < n - 1; i++) {
int f = i;
for (int j = i + 1; j < n; j++) {
if (a[j].compareTo(a[f]) < 0)
f = j;
}
swap(a, i, f);
}
}
public static int select(Comparable[] a, int n, int j, int f) {
if (j >= n)
return f;
if (a[j].compareTo(a[f]) < 0)
f = j;
return select(a, n, j + 1, f);
}
public static void selectionSort(Comparable[] a, int n, int i) {
if (i < n - 1) {
swap(a, i, select(a, n, i + 1, i));
selectionSort(a, n, i + 1);
}
}
public static void insertionSortItr(Comparable[] a) {
for (int i = 1; i < a.length; i++) {
int j;
Comparable cur = a[i];
for (j = i; j > 0 && cur.compareTo(a[j - 1]) < 0; j--) {
a[j] = a[j - 1];
}
a[j] = cur;
}
}
public static void insertionSortInner(Comparable[] a, Comparable cur, int j) {
if (j > 0 && cur.compareTo(a[j - 1]) < 0) {
a[j] = a[j - 1];
insertionSortInner(a, cur, j - 1);
} else {
a[j] = cur;
}
}
public static void insertionSort(Comparable[] a, int i, int n) {
if (i < n) {
insertionSortInner(a, a[i], i);
insertionSort(a, i + 1, n);
}
}
public static void main(String[] args) {
Integer[] a = new Integer[10];
for (int i = 0; i < 10; i++)
a[i] = (int) (Math.random()*100);
selectionSort(a, 10, 0);
for (int i = 0; i < 10; i++)
System.out.println(a[i]);
}
}

Bucket Sort Algorithm Code Issues

I need to implement the following in java.
Input: an array of integers
Output: Rearrange the array to have the following:
Suppose the first element in the original array has the value x
In the new array, suppose that x is in position I, that is data[I] = x. Then, data[j] <= x for all x and for all j > I. This means that all the values to the "left" of x are less than or equal to x and all the values to the "right" are larger than x.
An example is as follows: Suppose the array has the elements in this initial order: 4,3,9,2,7,6,5. After applying your algorithm, you should get: 3,2,4,5,9,7,6. That is, the leftmost element, 4, is positioned in the resulting array so that all elements less than 4 (2 and 3) are to its left (in no particular order), and all elements larger than 4 are to its right (in no particular order).
There is no space requirement for the algorithm, only that the problem is solved in O(n) time.
I have implemented a bucket sort, but am having some difficulties with the code.
public class Problem4
{
public static void partition(int[] A)
{
int x = A[0];
int l = A.length;
int[] bucket = int [];
for(int i=0; i<bucket.length; i++){
bucket[i]=0;
}
for (int i=0; i<l; i++){
bucket[x[i]]++;
}
int outPos=0;
for(int i=0; i<bucket.length; i++){
for(int j=0; j<bucket[i]; j++){
x[outPos++]=i;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] A = {4,3,9,2,7,6,5};
System.out.println("Before partition:");
for(int i = 0; i < A.length; i++)
{
System.out.print(A[i] + " ");
}
partition(A);
System.out.println("After partition:");
System.out.println("Before partition:");
for(int i = 0; i < A.length; i++)
{
System.out.print(A[i] + " ");
}
}
}
The lines:
int[] bucket = int[],
bucket[x[i]]++;, and
x[outpost++] = i;
are causing me troubles. I am getting the error
The type of expression must be an array type but is resolved to an int.
The problem stems from that first line where I am trying to create a new array called bucket. I would appreciate any suggestions! Thanks!
I don't think you need to resort to a bucket sort. Instead you can simply walk through the array, placing elements that are less than the split value at the front and elements that are greater at the back. We can use two variables, front and back to keep track of the insert position at the front and back of the array. Starting at position 1 in the array, if the value is less than the split value we place at front and increment front and the current index. If the value is greater we swap it with the value at back and decrement back, but we keep the current index.
Here's some code to illustrate:
public static void main(String[] args)
{
int[] A = {4,3,9,2,7,6,5};
sort(A);
System.out.println(Arrays.toString(A));
}
public static void sort(int[] arr)
{
int split = arr[0];
int front = 0;
int back = arr.length-1;
for(int i=1; front != back; )
{
if(arr[i] <= split)
{
arr[front] = arr[i];
front += 1;
i++;
}
else
{
swap(arr, i, back);
back -= 1;
}
}
arr[front] = split;
}
public static void swap(int[] arr, int i, int j)
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
Output:
[3, 2, 4, 7, 6, 5, 9]
Another approach you can use is to use the standard partition algorithm of QuickSort.
I have modified your code and the following code
public class Problem4
{
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
public static int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<high; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++;
// swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// swap arr[i+1] and arr[high] (or pivot)
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] A = {4,3,9,2,7,6,5};
System.out.println("Before partition:");
for(int i = 0; i < A.length; i++)
{
System.out.print(A[i] + " ");
}
// swap and call the standard partition algo of QuickSort
// on last element pivot. swap arr[low] and arr[high]
int low = 0;
int high = A.length-1;
int temp = A[low];
A[low] = A[high];
A[high] = temp;
partition(A, low, high);
System.out.println("\nAfter partition:");
for(int i = 0; i < A.length; i++)
{
System.out.print(A[i] + " ");
}
}
}
Outputs
Before partition:
4 3 9 2 7 6 5
After partition:
3 2 4 5 7 6 9
Hope it helps!

Quicksort (Random Pivot) code not sorting correctly Java

The code is a quicksort algorithm with a random pivot. It is not sorting the random array completely; I can't figure out why. The error seems to be with the bounds of the recursive calls and swapping.
public static <E extends Comparable<E>> void quickerSort(ArrayList<E> input, int lower, int higher){
int first = lower;
int last = higher;
if (lower >= higher)
return;
//generates the random pivot
int pivot = rand.nextInt(higher-lower+1) + lower;
//moves the pivot to the beginning of the list
E temp = input.get(first);
input.set(first, input.get(pivot));
input.set(pivot, temp);
pivot = first;
first++;
//if the values on the left side of the array are less than the
//value of the pivot, first is incremented (first part of loop) until
//a greater value is reached
//if the values on the right side of the array are greater than the
//value of the pivot, last is incremented until a lesser value is reached
while (first < last){
while (input.get(first).compareTo(input.get(pivot)) <= 0 && first < last){
first++;
}
while(input.get(last).compareTo(input.get(pivot)) > 0 && last >= first){
last--;
}
//switches the two values reached through the while loops
if (first < last){
temp = input.get(first);
input.set(first, input.get(last));
input.set(last, temp);
}
}
//moves the pivot to where first is
temp = input.get(first-1);
input.set(first-1, input.get(pivot));
input.set(pivot, temp);
//calls the method recursively
if (lower < first-1){
quickerSort(input,lower,first-1);
}
if (first < higher){
quickerSort(input,first,higher);
}
}
//implementing method
public static void testQuickSort(){
//creates a random list of integers to be sorted and prints it
ArrayList<Integer> list = new ArrayList<Integer>();
Random rand = new Random();
for (int i = 0; i <= 19; i++){
list.add(rand.nextInt(100));
System.out.print(list.get(i)+" ");
}
//calls quicksort and then prints the sorted array
System.out.println();
Assignment2.quickSort(list);
for (int i = 0; i <= 19; i++){
System.out.print(list.get(i)+" ");
}
}
Your code would not work for a simple input, {6, 0}, since your first and last will both point to the second element, therefore, 6 and 0 never get swapped.
I modified your code a little bit and it worked. I used variable names which make more sense here, but the logic is the same.
public static <E extends Comparable<E>> void quickSort(ArrayList<E> input, int low, int high) {
if (low >= high)
return;
// generates the random pivot
int pivotIndex = new Random().nextInt(high - low + 1) + low;
E pivot = input.get(pivotIndex);
// moves the pivot to the beginning of the list
Collections.swap(input, pivotIndex, low);
int i = low;
int j = high;
// if the values on the left side of the array are less than the
// value of the pivot, first is incremented (first part of loop) until
// a greater value is reached
// if the values on the right side of the array are greater than the
// value of the pivot, last is incremented until a lesser value is
// reached
while (i <= j) {
while (input.get(i).compareTo(pivot) <= 0 && i < j) {
i++;
}
while (input.get(j).compareTo(pivot) > 0 && j >= i) {
j--;
}
// break here when two pointers meet since the pivot position has been found
if (i >= j)
break;
// switches the two values reached through the while loops
Collections.swap(input, i, j);
}
// moves the pivot to where j is
Collections.swap(input, low, j);
// calls the method recursively
quickSort(input, low, j - 1);
quickSort(input, j + 1, high);
}

Issue with Recursive Randomized pivot sort

I'm having a bit of a problem writing a recursive function that sorts an array in java recursively . Right now it appears as though I have an infinite loop, and I can't seem to figure out where.
The primary function "rec_piv" searches from index1 to the pivot point and sorts the first half, then switches from the pivot point to the length of the array and sorts the second half. All of the comparisons are recorded by an int. [The array is of random size from 2 to 2014]
Thanks very much in advance!
public class Recursive_pivot {
private Random random_size = new Random();
private int size = random_size.nextInt(1024) + 2;
public int[] a = new int[size];
private Random elements = new Random();
/* variable for rec_piv */
public int temp=0;
public int temp2=0;
private Random rand_pivot = new Random();
private int pivot = rand_pivot.nextInt(size) + 2;
private int first_half =a[0+1];
private int second_half=a[pivot+1];
private int first_index=0; //first half of the array
private int second_index=pivot; //second half of the array
//The pivot is randomly chosen.
public int comparisons =0; //count the number of comparisons.
public void fill(){
for (int q=0; q<a.length; q++) {
/* filling the array */
a[q] = elements.nextInt(100 ) + 1;
}
}
public void rec_piv(int first_index, int second_index) {
if(first_index < pivot) {
if(first_half > a[first_index]) {
comparisons++;
temp = a[first_index];
a[first_index] = a[first_half];
a[first_half] = temp;
}
rec_piv(first_index+1, second_index);
}
if(second_index < a.length) {
if(second_half > a[second_index]) {
comparisons++;
temp2 = a[second_index];
a[second_index] = a[second_half];
a[second_half] = temp2;
}
rec_piv(first_index, second_index+1);
}
} //end of rec_piv
}//end of class.
You are trying to do a QSort here is a simple version of it.
public void quickSort(int array[], int start, int end)
{
int i = start; // index of left-to-right scan
int k = end; // index of right-to-left scan
if (end - start >= 1) // check that there are at least two elements to sort
{
int pivot = array[start];
while (k > i)
{
while (array[i] <= pivot && i <= end && k > i)
i++;
while (array[k] > pivot && k >= start && k >= i)
k--;
if (k > i)
swap(array, i, k);
}
swap(array, start, k);
quickSort(array, start, k - 1); // quicksort the left partition
quickSort(array, k + 1, end); // quicksort the right partition
}
else // if there is only one element in the partition, do not do any sorting
{
return; // the array is sorted, so exit
}
}
public void swap(int array[], int index1, int index2)
// pre: array is full and index1, index2 < array.length
// post: the values at indices 1 and 2 have been swapped
{
int temp = array[index1]; // store the first value in a temp
array[index1] = array[index2]; // copy the value of the second into the first
array[index2] = temp; // copy the value of the temp into the second
}
from this site.
http://www.mycstutorials.com/articles/sorting/quicksort

Quicksort partitioning

I have the following array:
int[] arr = { 19, 4, 2, 3, 9, 2, 10, 2, 7, 12, 5, 16, 8, 3, 11, 14, 0, 5 };
And now I use quicksort's partitioning to partition the array with pivot element 7:
public static void partition(int[] arr, int low, int high) {
int pivot = arr[low + (high - low) / 2];
int i = low;
int j = high;
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (arr[i] < pivot) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (arr[j] > pivot) {
j--;
}
// If we have found a values in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
swap(arr, i, j);
i++;
j--;
}
}
}
I am confused with the outcome:
5 4 2 3 0 2 3 2 5 12 7 16 8 10 11 14 9 19
I thought that every element <= pivot (7) must be to the left and every element > than pivot element must be on the right. But why is 12 left to 7 ?
This implementation cannot guarantee what you would expect. All it does is the following (provided that you change to arr[i] <= pivot, as Achintya Jha suggested, otherwise it can't even guarantee that):
For every pair of values a, b with a <= pivot < b, it is guaranteed that a will be left of b in the end. However, you don't guarantee anything about the exact position of pivot in the final array (only that it's left of all values that are larger).
A partition function in C++ looks like this:
while (first!=last) {
while (pred(*first)) {
++first;
if (first==last) return first;
}
do {
--last;
if (first==last) return first;
} while (!pred(*last));
swap (*first,*last);
++first;
}
return first;
First and last are iterators that point to elements in the array, analogous to your i and j variables. pred is short for predicate, which could be i <= 7 for example. Essentially this function returns the midpoint, so in C++ code, you would get all elements to the left of the midpoint by iterating up to it, and all elements to the right by iterating from it to the end. To make it less confusing:
i should be the first element, j should be the last element.
// get midpoint first
...
// Note. <= is the opposite of >
// Which logically is the same as
// pred is the opposite of !pred
while (i != j) {
while (i <= midpoint) {
++i;
if (i == j) return i;
}
do {
--j;
if (i == j) return i;
} while (i > midpoint);
swap (i, j);
++i;
}
return i;
...
for (int i = 0; i < get_midpoint(...); i++)
for (int i = get_midpoint; i < end_of_array; i++)
Create a class for the quicksort method
package com.Ramesh;
public class QuickSort {
public void sort(int[] a,int left,int right){
if(left<right)
{
int partition=getPartition(a, left, right);
sort(a,left,partition-1);
sort(a,partition+1,right);
}
}
public int getPartition(int[] a,int l,int r)
{
int pivot=a[l];
int left=l;
int right=r;
while(left<right)
{
while(a[left]<pivot){
left++;
}
while(a[right]>pivot){
right--;
}
if(left<right){
int temp=a[left];
a[left]=a[right];
a[right]=temp;
}
}
return right;
}
}
2.Create another class to invoke the sort method
import java.util.Scanner;
public class Execute {
private int[] a;
private int len;
public int[] getA() {
return a;
}
public void setA(int[] a) {
this.a = a;
}
public int getLen() {
return len;
}
public void setLen(int len) {
this.len = len;
}
public static void main(String[] args) {
Execute ex=new Execute();
ex.takeInput();
QuickSort q=new QuickSort();
q.sort(ex.getA(),0,ex.getLen()-1);
System.out.println("Printing the the Sorted Object");
System.out.println(ex);
}
public void takeInput()
{
Scanner s1=new Scanner(System.in);
System.out.println("Please enter the no of element to be sorted");
len=s1.nextInt();
a=new int[len];
System.out.println("Pls enter the elements");
for(int i=0;i<len;i++){
a[i]=s1.nextInt();
}
}
#Override
public String toString(){
StringBuffer s=new StringBuffer("");
for(int i=0;i<this.len;i++){
s.append(this.a[i]+"\n");
}
return s.toString();
}
}

Categories