13 Replies - 416 Views - Last Post: 22 October 2012 - 04:59 AM
#1
adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 11:27 AM
I tried to work it on the paper but I still have not much clue on how I can compare element in the array to the one in list so I can correctly add them in order...
Replies To: adding elements in array to list in descending order using ListIterate
#2
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 11:46 AM
What do you mean to compare from an array to a list to add in order ?
#3
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 11:53 AM
#4
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 11:56 AM
The easiest way, that takes only a line of code, is to first sort the array
#5
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 12:04 PM
#6
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 01:11 PM
erm.. but when I read the spec sheet again, look like this: for each integer x in intArray, use list iterator to iterate over elements in list and find position to insert x....
#7
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 01:20 PM
Ok now that makes sense
Just iter in you array and them iter in your link list to insert at the good place
If you develop your own LinkedList just have a method insertInOrder() which will insert after the node returned by the method getLargestSmallerThan(int number)
Just iter in you array and them iter in your link list to insert at the good place
If you develop your own LinkedList just have a method insertInOrder() which will insert after the node returned by the method getLargestSmallerThan(int number)
#8
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 01:24 PM
pbl, on 21 October 2012 - 01:20 PM, said:
Ok now that makes sense
Just iter in you array and them iter in your link list to insert at the good place
If you develop your own LinkedList just have a method insertInOrder() which will insert after the node returned by the method getLargestSmallerThan(int number)
Just iter in you array and them iter in your link list to insert at the good place
If you develop your own LinkedList just have a method insertInOrder() which will insert after the node returned by the method getLargestSmallerThan(int number)
Here is my code if I arrange the array in order then flip it backward:
private static void insertIntoLinkedList(Integer[] intArray, LinkedList<Integer> list)
{
//create iterator object
ListIterator it = list.listIterator();
//sort array in ascending order
Arrays.sort(intArray);
//flip it backward and now in descending order
for(int i = 0; i < intArray.length/2; i++)
{
int temp = intArray[i];
intArray[i] = intArray[intArray.length - i - 1];
intArray[intArray.length - i - 1] = temp;
}
int list_size = list.size();
//add into list using iterator
for(int i =0;i<list_size;i++)
{
}
}
And I cant make new methods, all I can do is fill in the empty method. I import the class list and LinkedList, so I dont have my own methods for the list.
What trouble me is that I have to manipulate the iterator by next or previous, after a while doing it on paper, I kinda confused on how using it.
This post has been edited by RozenKristal: 21 October 2012 - 01:26 PM
#9
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 01:45 PM
I don't think this is what your teacher wants
He wants, if I understand well:
He wants, if I understand well:
for(int i = 0; i < array.length; ++i) {
Iterator iter = linkList.listIterator();
while(iter has element) {
fetch element
if(element > array[i]) {
insert array[i] there
break;
}
}
#10
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 01:50 PM
thank pbl, I will try it and see how it goes, if there more questions popup, I will check back with the forum 
One more question though, seem like in the code, every time the outer loop starts, you restart iter to first position?
One more question though, seem like in the code, every time the outer loop starts, you restart iter to first position?
This post has been edited by RozenKristal: 21 October 2012 - 01:53 PM
#11
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 02:07 PM
Never do it the way the teacher wants
The Collections class offers some good methods to do what you want
The Collections class offers some good methods to do what you want
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
public class Snippet {
public static void main(String[] args) {
new Snippet();
}
public Snippet() {
Integer[] array = { 1, 4, 1, 60, 30, 75, 31 };
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(5);
list.add(2);
list.add(30);
insertIntoLinkedList(array, list);
for (Integer integer : list)
System.out.println(integer);
}
private void insertIntoLinkedList(Integer[] intArray, LinkedList<Integer> list) {
for (Integer integer : intArray)
list.add(integer);
Collections.sort(list, new ReverseOrder());
}
private class ReverseOrder implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 == o2)
return 0;
else if (o1 < o2)
return 1;
else
return -1;
}
}
}
#12
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 05:13 PM
Because there is the word "MUST". I dont really want to lose points over that haha
#13
Re: adding elements in array to list in descending order using ListIterate
Posted 21 October 2012 - 08:33 PM
I encounter problem when I put the code together, cuz the list is null in the beginning, hence it cant iterate... what can I do?
for (int index =0;index<array.length;index++)
{
ListIterator<Integer> iterate = list.listIterator();
while (iterate.hasNext())
{
int current = iterate.next();
if(current>array[index])
{
iterate.previous();
iterate.add(array[index]);
break;
}
}
}
#14
Re: adding elements in array to list in descending order using ListIterate
Posted 22 October 2012 - 04:59 AM
Isn't it the callers job to make sure the list isn't null? Else just make a new instance of a LinkedList
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|