QUESTIONS
Let list1 and list2 contain arbitrary number of items. Describe briefly what the following code snippet does:
while (![login to view URL]()) {
[login to view URL]([login to view URL](), [login to view URL](0));
}
Suppose the design requirement for the following code is to remove every other item from a given list.
for (int i = 0; i < [login to view URL](); i += 2) {
[login to view URL](i);
}
If colors initially contains ["violet", "indigo", "blue", "green", "yellow", "orange", "red"], then trace the code on this input by showing the value of i, value of [login to view URL]() and contents of colors for each execution of the for loop body. To receive full credit, you should start by showing the initial values before the loop execution begins and then show results after each execution of the for loop, until you show the final results. Use this format for your answer: i = unInitialized, [login to view URL]() = ..., colors = [...] and then i = 0, ...
Does the code work as per the design requirement? Explain your answer briefly.
Assume the following:
The Iterator<E> interface is as discussed in lecture.
The ArrayList<E> class implements the Iterable<E> and ListADT<E> interfaces.
The items in a list can be compared with the equals method or the compareTo method.
Complete the Java method specified below, making use of iterators Hint: You would need a lot of them!. Assume this method is implemented in a main class. To receive full credit, your solution:
must explicitly use iterators for traversing lists
must not use the contains method
must not modify the contents of the parameter
public static <E> ArrayList<E> flatten(ArrayList<ArrayList<E>> list) {
// If list is null, throw a NullPointerException.
// If list is empty, return a new empty list.
// Otherwise create and return a new list that contains the objects contained in
// any of the lists contained in list, removing duplicate items. The order of
// items in the returned list should be the same as the order in which they
// first appear in the original list.
//Example: list: [[1,2,3], [4,3,6], [1,5]] result: [1,2,3,4,6,5]
First answer: You move all item from list1 to list2. Remove
Second answer: the code will node work as design because the size method will change everytime you remove an element.
Third answer:
public static <E> ArrayList<E> flatten(ArrayList<ArrayList<E>> list) {
ArrayList<E> result = new ArrayList<>();
if (list == null) {
throw new NullPointerException();
}
for (ArrayList<E> l : list) {
[login to view URL](l);
}
return result;
}
₹100 INR en 40 días
4,6 (5 comentarios)
2,2
2,2
17 freelancers están ofertando un promedio de ₹275 INR /hora por este trabajo
Good Day!
i'm expert java dev with more than 7 years experience in IT area working with cutting edge technologies for both web application and software development.
Kindly Regards
I've been writing codes in Python for 4 years and I can get the task done. I'm very good with DS, algorithms and have no problem converting any ideas into code
hey this is kartikeya, i have 3+ years of experience in c/c++. I have done many projects for students here. Check my reviews. I have read the questions here and can do the work
Ping me
I am expertise in data structures,c,c++,java programming languages from past 4 years in IT software industry.I have hands on experience on data structures in real time banking applications.
I assume this a students homework. Providing an answer that doesn't help you to learn is not in your own best interest. Make a good faith attempt to solve the problem yourself first and ask about specific problems with your existing implementation. If your school has a policy regarding outside help on homework, make sure you are aware of it before you receive help. Never use code you don't understand. It definitely won't help you later (after school, in later assignments, on tests, etc.) and it could be, at best, very embarrassing if you are asked to explain code you turned in.