using Java Hello I need to change this code to use ArrayList

 (using Java) 
 Hello, I need to change this code to use ArrayList insted of Array  code below that I need to change   
 import java.util.Arrays; import java.util.*; import java.lang.*; import java.io.*; public class ArrayOperations {        public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println(\"********************\");        show(new int[]{1,2,3}); show(new int[]{}); System.out.println(\"********************\"); int c[] = reverse(new int[]{1,2,3}); System.out.println(\"Array Elements in reverse: \"+Arrays.toString(c)); System.out.println(\"********************\"); int max[] = max(new int[]{5,-10,7}, new int[]{10,-5,7,12,-14,0}); System.out.println(\"Max Array elements are: \"+Arrays.toString(max)); System.out.println(\"********************\"); boolean status = allSame(new int[]{5,5,5,5}); System.out.println(status); status = allSame(new int[]{5,5,4,5}); System.out.println(status); status = allSame(new int[]{5}); System.out.println(status); System.out.println(\"**********allDifferent check***********\");        System.out.println(allDifferent (new int []{ 5,10,2,11} ));        System.out.println(allDifferent (new int []{5,10,2,10} ));        System.out.println(\"**********increasing check***********\");        System.out.println(increasing (new int []{ 1,2,30}) );        System.out.println(increasing (new int []{1,2,-30}) );        System.out.println(increasing (new int []{5,5,6} ));        System.out.println(increasing (new int []{}) );        System.out.println(\"**********nonzero check***********\");        int d[] = nonzero (new int []{ 1,2,3}) ;        System.out.println(Arrays.toString(d));        d = nonzero (new int []{1,0,-2,0,3});        System.out.println(Arrays.toString(d));        d = nonzero (new int []{0,0,0,0,0,0,0} );        System.out.println(Arrays.toString(d));        System.out.println(\"**********withoutRepeats check***********\");        d = withoutRepeats (new int []{ 1,1,1,1,2,2,8,1,7,7,7} );        System.out.println(Arrays.toString(d));  System.out.println(\"**********Status***********\");        double ans[] = stats(new double []{1.0,2.0,3.0,4.0, 5.0,6.0});        for(int i=0;i<5;i++)            System.out.println(ans[i]);                     System.out.println(\"**********Concat***********\");         System.out.println(concat(new String []{\"She\",\"sells\",\"sea\",\"shells\"}));                  System.out.println(\"**********Find***********\");        System.out.println(find (\"blah\", new String []{\"Blah\",\"blah\",\"blah\"}));                String a[] = new String []{\"Blah\",\"blah\",\"blah\"};       // reverse(a);        for(int i=0;i<a.length;i++)       {           System.out.println(a[i]);       }        System.out.println(\"********************\");       }        public static void show(int[]a){        String startStr = \"{\";        String endString = \"}\";        String s = \"\";        for(int i=0; i<a.length; i++){            if(i != a.length-1)            s = s + a[i] + \",\";            else            s = s + a[i];          }        s = startStr + s + endString;        System.out.println(s);           }    public static int[] reverse(int[]a) {        int b[] = new int[a.length];        for(int i=a.length-1, j=0; i>=0; i--, j++){            b[j] = a[i];        }        return b;    }    public static int[] max(int[]a,int []b){        int length= 0;        if(a.length > b.length)            length = a.length;        else if(b.length > a.length)            length = b.length;        else            length = a.length;            int c[] = new int[length];            for(int i=0; i<c.length; i++){                if(c.length == a.length){                    c[i] = a[i];                }                else{                    c[i] = b[i];                }            }            return c;    }    public static boolean allSame(int[] a){        if(a.length == 0 || a.length == 1)            return true;        else{            for(int i=0; i<a.length-1; i++){                if(a[i] != a[i+1])                    return false;            }        }        return true;    }    public static boolean allDifferent (int[] a){        if(a.length == 1 || a.length == 0)            return true;        else{            for(int i=0; i<a.length; i++){                for(int j=i+1; j<a.length; j++){                if(a[i] == a[j])                    return false;                }            }        }        return true;    }    public static boolean increasing (int[] a){        if(a.length == 1 || a.length == 0)            return true;        else{            for(int i=0; i<a.length-1; i++){                if(a[i] > a[i+1])                    return false;            }        }        return true;           }    public static int[] nonzero (int [] a){ //ask about this one        int nonZeroCount = 0;        for(int i=0; i<a.length; i++){            if(a[i] != 0){                nonZeroCount++;            }        }        int b[] = new int[nonZeroCount];        for(int i=0, j =0; i<a.length; i++){            if(a[i]!=0){                b[j++] = a[i];            }        }        return b;    }    public static int[] withoutRepeats (int[] a){ //ask about this one        int nonconsecutiveCount = 1;        for(int i=0; i<a.length-1; i++){            if(a[i] != a[i+1]){                nonconsecutiveCount++;            }        }        int b[] = new int[nonconsecutiveCount];        int j =0;        for(int i=0; i<a.length-1; i++){            if(a[i] != a[i+1] ){                b[j++] = a[i];            }        }        b[j] = a[a.length-1];        return b;    }       public static double [] stats (double [] a)    {        if(a==null) // retrun if a is null;            return null;        if(a.length==0) // return if a has length 0;            return null;        double min_v = a[0]; // to find minimum        double avg_v = 0.0; // to find average        double max_v = a[0]; // to find maximum        double sum_v = a[0]; // to find sum        double prod_v = a[0]; // to find product                      for(int i=1;i<a.length;i++)        {            min_v = Math.min(min_v,a[i]);            max_v = Math.max(min_v,a[i]);            sum_v += a[i];            prod_v *= a[i];        }        avg_v = sum_v/a.length;        return new double[]{min_v,avg_v,max_v,sum_v,prod_v};    }           public static String concat(String [] a)    {        String ans = \"\";        for(int i=0;i<a.length;i++)        {            ans = ans+a[i];        }        return ans;    }        public static int find(String needle, String [] haystack)    {        for(int i=0;i<haystack.length;i++)        {            if(haystack[i].compareTo(needle)==0) // if match found return the index                 return i;        }        return -1;     } } 

Solution

The complete code in which I changed every array with ArrayList is as follows:

Source Code:

import java.util.Arrays;
import java.util.*;

public class ArrayOperations {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(\"********************\");

//ArrayList<> means ArrayList<Integer> as the arguments are integer.
//This is known as diamond interface <>.
show(new ArrayList<>(Arrays.asList(1,2,3)));
  
show(new ArrayList<>());
// It is same as show(new ArrayList<Integer>()) because in function definition, Integer is mentioned.

System.out.println(\"********************\");
ArrayList<Integer> l1 = reverse(new ArrayList<>(Arrays.asList(1,2,3)));
System.out.println(\"ArrayList Elements in reverse: \" + l1.toString());
System.out.println(\"********************\");
  
l1 = max(new ArrayList<>(Arrays.asList(5,-10,7)), new ArrayList<>(Arrays.asList(10,-5,7,12,-14,0)));
System.out.println(\"Max Array elements are: \" + l1.toString());
System.out.println(\"********************\");
boolean status = allSame(new ArrayList<>(Arrays.asList(5,5,5,5)));
System.out.println(status);
status = allSame(new ArrayList<>(Arrays.asList(5,5,4,5)));
System.out.println(status);
status = allSame(new ArrayList<>(Arrays.asList(5)));
System.out.println(status);
System.out.println(\"**********allDifferent check***********\");
System.out.println(allDifferent(new ArrayList<>(Arrays.asList(5,10,2,11))));
System.out.println(allDifferent(new ArrayList<>(Arrays.asList(5,10,2,10))));
System.out.println(\"**********increasing check***********\");
System.out.println(increasing(new ArrayList<>(Arrays.asList(1,2,30))));
System.out.println(increasing(new ArrayList<>(Arrays.asList(1,2,-30))));
System.out.println(increasing(new ArrayList<>(Arrays.asList(5,5,6))));
System.out.println(increasing(new ArrayList<>()));
System.out.println(\"**********nonzero check***********\");
ArrayList<Integer> d = nonzero(new ArrayList<>(Arrays.asList(1,2,3)));
System.out.println(d.toString());
d = nonzero(new ArrayList<>(Arrays.asList(1,0,-2,0,3)));
System.out.println(d.toString());
d = nonzero(new ArrayList<>(Arrays.asList(0,0,0,0,0,0,0)));
System.out.println(d.toString());
System.out.println(\"**********withoutRepeats check***********\");
d = withoutRepeats(new ArrayList<>(Arrays.asList(1, 1, 1, 1, 2, 2, 8, 1, 7, 7, 7)));
System.out.println(d.toString());
System.out.println(\"**********Status***********\");
ArrayList<Double> ans = stats(new ArrayList<>(Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)));
for (int i = 0; i < 5; i++) {
System.out.println(ans.get(i));
}

System.out.println(\"**********Concat***********\");

System.out.println(concat(new ArrayList<>(Arrays.asList(\"She\", \"sells\", \"sea\", \"shells\"))));

System.out.println(\"**********Find***********\");
System.out.println(find(\"blah\", new ArrayList<>(Arrays.asList(\"Blah\", \"blah\", \"blah\"))));

ArrayList<String> a = new ArrayList<>(Arrays.asList(\"Blah\", \"blah\", \"blah\"));
// reverse(a);
for (int i = 0; i < a.size(); i++) {
System.out.println(a.get(i));
}
System.out.println(\"********************\");

}

public static void show(ArrayList<Integer> a) {
String startStr = \"{\";
String endString = \"}\";
String s = \"\";
for (int i = 0; i < a.size(); i++) {
if (i != a.size() - 1) {
s = s + a.get(i) + \",\";
} else {
s = s + a.get(i);
}
}
s = startStr + s + endString;
System.out.println(s);

}

public static ArrayList<Integer> reverse(ArrayList<Integer> a) {
ArrayList<Integer> b = new ArrayList<>(a.size());
for (int i = a.size() - 1; i >= 0; i--) {
b.add(a.get(i));
}
return b;
}

public static ArrayList<Integer> max(ArrayList<Integer> a, ArrayList<Integer> b) {
int length = 0;
ArrayList<Integer> c;
if (a.size() > b.size()) {
c = new ArrayList<>(a);
} else if (b.size() > a.size()) {
c = new ArrayList<>(b);
} else {
c = new ArrayList<>(a);
}
return c;
}

public static boolean allSame(ArrayList<Integer> a) {
if (a.isEmpty() || a.size() == 1) {
return true;
} else {
for (int i = 0; i < a.size() - 1; i++) {
if (a.get(i) != a.get(i + 1) ) {
return false;
}
}
}
return true;
}

public static boolean allDifferent(ArrayList<Integer> a) {
if (a.size() == 1 || a.isEmpty()) {
return true;
} else {
for (int i = 0; i < a.size(); i++) {
for (int j = i + 1; j < a.size(); j++) {
if (a.get(i) == a.get(j)) {
return false;
}
}
}
}
return true;
}

public static boolean increasing(ArrayList<Integer> a) {
if (a.size() == 1 || a.isEmpty()) {
return true;
} else {
for (int i = 0; i < a.size() - 1; i++) {
if (a.get(i) > a.get(i + 1)) {
return false;
}
}
}
return true;

}

public static ArrayList<Integer> nonzero(ArrayList<Integer> a) { //ask about this one
int nonZeroCount = 0;
for (int i = 0; i < a.size(); i++) {
if (a.get(i) != 0) {
nonZeroCount++;
}
}
ArrayList<Integer> b = new ArrayList<>(nonZeroCount);
for (int i = 0; i < a.size(); i++) {
if (a.get(i)!= 0) {
b.add(a.get(i));
}
}
return b;
}

public static ArrayList<Integer> withoutRepeats(ArrayList<Integer> a) { //ask about this one
int nonconsecutiveCount = 1;
for (int i = 0; i < a.size() - 1; i++) {
if (a.get(i) != a.get(i + 1)) {
nonconsecutiveCount++;
}
}
ArrayList<Integer> b= new ArrayList<>(nonconsecutiveCount);
for (int i = 0; i < a.size() - 1; i++) {
if (a.get(i) != a.get(i + 1)) {
b.add(a.get(i));
}
}
b.add(a.get(a.size() - 1));
return b;
}

public static ArrayList<Double> stats(ArrayList<Double> a) {
if (a == null) // retrun if a is null;
{
return null;
}
if (a.isEmpty()) // return if a has length 0;
{
return null;
}
double min_v = a.get(0); // to find minimum
double avg_v = 0.0; // to find average
double max_v = a.get(0); // to find maximum
double sum_v = a.get(0); // to find sum
double prod_v = a.get(0); // to find product

for (int i = 1; i < a.size(); i++) {
min_v = Math.min(min_v, a.get(i));
max_v = Math.max(min_v, a.get(i));
sum_v += a.get(i);
prod_v *= a.get(i);
}
avg_v = sum_v / a.size();
return new ArrayList<Double>(Arrays.asList(min_v, avg_v, max_v, sum_v, prod_v));
}

public static String concat(ArrayList<String> a) {
String ans = \"\";
for (int i = 0; i < a.size(); i++) {
ans = ans + a.get(i);
}
return ans;
}

public static int find(String needle, ArrayList<String> haystack) {
for (int i = 0; i < haystack.size(); i++) {
if (haystack.get(i).compareTo(needle) == 0) // if match found return the index
{
return i;
}
}
return -1;

}
}

Description:

Here, to convert the array in the arrayList, basically you can use a method called Arrays.asList() and pass the arguments to it which were the values you want to store inside it.

One more thing changes is instead of a[i], you require to write a.get(i) as it is an ArrayList.

To create an ArrayList with specific size, you need to pass it in the parameter. This is used in method withoutRepeats(). In every method, the concept is same.

If you want to copy the array a into b completely, there are 2 methods which does this, using b[j] = a[i] something like this. So, you can replace it with b.add(a.get(i)) if you don\'t want to copy the whole arraylist. (see method withoutRepeats() for this.)

If you want to completely copy the array into another, you can pass the arraylist as a parameter in : new ArrayList(parameter). See the method max() in which I have created \'c\' arraylist using \'a\' and \'b\' as per the requirements.

To check whether array is empty, previously it was using a.length==0. But, in arraylist, you can replace it with a.isEmpty() which is same as this condition. And you had used Arrays.asString(array) to print it inside println() method. Now, with an arrayList, you can write it as l1.toString().

You do not need to change a single line to change for logical things, you just have to convert every array in arraylist. (However, you can\'t replace args[] array from main(): public static void main(String [] args) because it won\'t be allowed to run the program.)

This code is working proper, I checked the output as well. Do comment if there is any query. Thank you. :)

 (using Java) Hello, I need to change this code to use ArrayList insted of Array code below that I need to change import java.util.Arrays; import java.util.*; i
 (using Java) Hello, I need to change this code to use ArrayList insted of Array code below that I need to change import java.util.Arrays; import java.util.*; i
 (using Java) Hello, I need to change this code to use ArrayList insted of Array code below that I need to change import java.util.Arrays; import java.util.*; i
 (using Java) Hello, I need to change this code to use ArrayList insted of Array code below that I need to change import java.util.Arrays; import java.util.*; i
 (using Java) Hello, I need to change this code to use ArrayList insted of Array code below that I need to change import java.util.Arrays; import java.util.*; i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site