Question: Hi guys, I really need some help here. I want someone to look over my answer, and see if I'm on the right track. This

Hi guys,

I really need some help here. I want someone to look over my answer, and see if I'm on the right track. This is for a Java class

Here the assignment:

1. Describe auto-boxing, including why it is useful. (Google for this one) make a few lines of code that auto-box an int into an Integer, and un-box an Integer to an int.

2. Declare an ArrayList of Strings. Add 5 names to the collection. "Bob" "Susan" ... Output the Strings onto the console using the enhanced for loop.

3. Sort the list using the method Collections.sort. Output the sorted List. Shuffle the list, and output the shuffled list. Note that Collections (with an s) is a class, while Collection is an interface. The Collections class has many useful static methods for processing interfaces, including the sort method.

4. Search for the name "Susan" in the list. What location was it found? Search for a name that is not in the list. What location is reported?

5. Describe why an equals method and a compareTo method are required to achieve searching and sorting of the elements of a list.

6. Convert the list above to an array using toArray. Output the elements of the array. Convert the array back into a list using asList. Output the elements of the list.

  1. The auto-boxing does the following:
  • In calling to a function where the object is excepted but the primitive data values is passed then the automatically converts from the promivtive to object.
  • Using the auto-boxing, we can store primitive types in collection framework like arrays or arrayslist.
  • code to showing auto-boxing

ingterger rr=10; autoboxing - primitive object

  • code to show unboxing

int ee = 10; unboxing in java.

2 .

import java.util.ArrayList;

import java.util. Arrays;

public class AutoBoxingandUnboxing {

public static void main(String[] args){

Arraylist Intlist = new ThreadLocal ();

intList.add(1);

int.List.add(2);

ThreadLocalintLocal = New ThreadLocal();

intLocal.set(4);

ArrayList names = new ArrayList(Array.aslist("Bob","Susan","Carla","Elliot","JD"))

for(String name: names){

system.out.println(name);

}

3 .

ArrayList names = new ArrayList(Array.aslist("Bob","Susan","Carla","Elliot","JD"))

collections.sort(sortNames)

system.out.println(sortNames)

4 .

int index = seaNames.indexof("Susan");

if(index != -1){

system. out.println("found susan at index:" + index)

} else{

system.out.println("susan has been eating by the internet");

}

int indexNoSusan = seaNames.indexOf("Goats");

if(indexNoSusan != -1){

system.out.println("found Goats at index: " + indexNoSusan);

}else{

system.out.println("goats started to scream");

}

5

  • Equal will take any Objects as a paramiter, but compareTO will only take strings.
  • Equals only tell you weather if he the Object is equal or not, but compareTo give mor information on the string compare.

6 .

ArrayList backList = new ArrayList(Array.aslist("Bob","Susan","Carla","Elliot","JD"))

backList.add("Bob");

backList.add("susan");

backList.add("Carla");

backList.add("Elliot");

backList.add("JD");

String[] stocArr = new String[backList.size()]

stocArr = backList.toArray(stocArr);

system.out.println(s);

}

ArrayList reBuild = new ArrayList(Array.aslist("Bob","Susan","Carla","Elliot","JD"))

for(String list: reBuild){

system.out.println(list);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!