Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What does the array list myInts contain at the end of this code segment? ArrayLi

ID: 3545398 • Letter: W

Question

  1. What does the array list myInts contain at the end of this code segment?
         ArrayList myInts = new ArrayList();
    int temp = 1;

    for (int i = 1; i < 10; i++) {
    myInts.add(temp);
    temp = temp * i;

    }
    myInts.remove(0);
    myInts.remove(2);
  2. What does this method do? In one short English sentence describe the task accomplished by this method.
         public static ArrayList<Integer> foo(ArrayList<Integer> a)
    {
    ArrayList<Integer> b = new ArrayList<Integer>();
    for (int i = 0; i < a.size(); i++) {
    if (a.get(i) < 0) {
    b.add(i);
    }
    }
    return b;
    }
  3. What does this method do? In one short English sentence describe the task accomplished by this method.
         public static ArrayList<String> bar(ArrayList<String> a)
    {
    ArrayList<String> tmp = new ArrayList<String>();

    for (int i = 0; i < a.size(); i++) {
    if (a.get(i).length() > 10) {
    tmp.add(a.get(i));
    }
    }
    return tmp;
    }
  4. Write a static method named concatenate that takes two ArrayLists of Strings as a parameter and returns a new ArrayList of Strings that contains all of the elements of the first list followed by all of the elements of the second list.
  5. Write a static method named firstDigit that takes an ArrayList of Strings as a parameter and returns a new ArrayList of Strings that contains only the elements of the first list that start with a digit.  (Recall the method Character.isDigit() from previous homework/lab/lecture session?                                                                                                                                                                                                                                                        /I hope there will be quotes explaining how to solve this problems, thanks!

Explanation / Answer

1.

public class ArrayList {

    public static void main(String[] args) {
        java.util.ArrayList myInts = new java.util.ArrayList();
        int temp = 1;

        for (int i = 1; i < 10; i++) {
            myInts.add(temp);
            temp = temp * i;

        }
        myInts.remove(0);
        myInts.remove(2);
        System.out.println(myInts);

   }

}

[1, 2, 24, 120, 720, 5040, 40320]

2.

Returning the list of indexes of List "a" whose values are negative(less than 0)

3.

Returning the list of Strings which has more than 10 characters(length is greater than 10)

4.

public class ArrayList {

public static void main(String[] args) {
java.util.ArrayList<String> a = new java.util.ArrayList<String>();
java.util.ArrayList<String> b = new java.util.ArrayList<String>();
a.add("hello");
a.add("how");
b.add("r");
b.add("u?");
System.out.println(concatenate(a, b));
}

public static java.util.ArrayList<String> concatenate(java.util.ArrayList<String> a, java.util.ArrayList<String> b) {
java.util.ArrayList<String> result = new java.util.ArrayList<String>();
int i = 0;
for (i = 0; i < a.size(); i++) {
result.add(i, a.get(i));
}

for (int j = 0; j < b.size(); j++) {
result.add((i + j), b.get(j));
}
return result;
}
}

5

public class ArrayList {

public static void main(String[] args) {
java.util.ArrayList<String> a = new java.util.ArrayList<String>();
a.add("1hello");
a.add("how");
a.add("2r");
a.add("3u?");
System.out.println(a.size());
System.out.println(firstDigit(a));
}

public static java.util.ArrayList<String> firstDigit(java.util.ArrayList<String> a) {
java.util.ArrayList<String> result = new java.util.ArrayList<String>();
int j=0;
for (int i = 0; i < a.size(); i++) {
String temp = a.get(i);
if(Character.isDigit(temp.charAt(0))){
result.add(j++, a.get(i));
}
}
return result;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote