Question

In: Computer Science

Move all zeros in an arraylist to the end of the list using a temp(java). if...

Move all zeros in an arraylist to the end of the list using a temp(java).

if it is an array the code would look like the code below, change it to fit an arraylist

int count = 0;

    int temp;

    for (int i = 0; i < n; i++) {

    if ((arr[i] != 0)) {

        temp = arr[count];

        arr[count] = arr[i];

        arr[i] = temp;

        count = count + 1;

    }

    }

}

Solutions

Expert Solution

import java.util.ArrayList;

public class MyClass {
public static void main(String[] args) {
ArrayList<Integer> cars = new ArrayList<Integer>();
cars.add(0); cars.add(2); cars.add(3); cars.add(0);
cars.add(3); cars.add(0); cars.add(3);
System.out.println("Before: " + cars);
  
int i=0, count =0;
while(i < cars.size())
{
if(cars.get(i) == 0)
{
cars.remove(i);
i--;
count++;
}
i++;
}
while(count-- > 0)
cars.add(0);
System.out.println("After: " + cars);
}
}


Related Solutions

Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");             list.add("Sauer");             list.add("Maynard");             list.add("Namath");             list.add("Boozer");             list.add("Snell");             list.add("Namath");             list.add("Atkinson");             list.add("Lammonds");             list.add("Dockery");             list.add("Darnold");             list.remove(2);             list.set(2, "Brady");             list.remove(2);             list.set(4,"Unitas");             list.add(1,"Lamomica");             list.add(3,"Hanratty");             list.remove("Namath");             list.remove(list.size()-1);             list.remove(2);             list.set(7, "Riggins");             Iterator iter = list.iterator();           while (iter.hasNext())         {   System.out.print(iter.next() + " ");                         }                     } }
List all possible rational zeros given by the Rational Zeros Theorem (but don't check to see...
List all possible rational zeros given by the Rational Zeros Theorem (but don't check to see which actually are zeros). (Enter your answers as a comma-separated list.) Q(x) = x4 − 5x3 − 3x + 6
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList...
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList are the same data structure but implement data storage in different ways. Classes, that implement Map interface in Java collections framework are used for storing what type of data? Declare and instantiate a list of elements of type String. Name this list myArray. what type of data structure is Stack? LinkedList data structure in Java collections is implemented as doubly linked lists. In PriorityQueue...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with no imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as...
Using Java with NO imports,    Method arrayOfSums returns an ArrayList of the same size as the largest ArrayList        This method needs to be overloaded so that it works for 2 to 4 sent Arraylists.        arrayOfSums([2,4,7], [3,7,14]) returns [5,11,21]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3]) returns [15, 23, 21, 11]        arrayOfSums([6,13,4,8], [7,5,9], [2,5,8,3], [3,5,4,1,2]) returns [18, 28, 25, 12, 2]        arrayOfSums([8,6], [2,4,10]) returns [10,10,10]        arrayOfSums([2,2,2,2,2], [3,2,5,2,3], [1,3,1,3,1]) returns [6, 7, 8,...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]] [ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c] assuming abc are integer Thanks in advance!
In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from...
In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from some input text file: Total number of characters used,without counting spaces and punctuation, total number of words used; Number of words, counting each word only once; Total number of punctuation characters;Number of words that are of size six or more;Number of words that are used only once
1. List the possible rational zeros of the the polynomial function. Do not find the zeros....
1. List the possible rational zeros of the the polynomial function. Do not find the zeros. f ( x ) = 3 x 5 − 5 x 2 + 6 x − 1 A) ± 1 , ± 3 B) ± 1 , ± 1 3 C) ± 1 , ± 1 3 , ± 3 D) ± 3 , ± 1 3 Group of answer choices A B C D 2. Find zeros and write the polynomial in factored...
At a temp below room temp but with all components as gases, the decomposition of iodine...
At a temp below room temp but with all components as gases, the decomposition of iodine monochloride, ICl, to iodine and chlorine, is 1.4*10^-5 2ICl (g)----> I2 (g) + Cl2 (g) If a sealed vessel has [ICl]=1.87 M, and no iodine or chlorine. What are the equilibrium cocentrations?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT