13 Replies - 1101 Views - Last Post: 22 October 2012 - 04:59 AM Rate Topic: -----

#1 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

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...
Is This A Good Question/Topic? 0
  • +

Replies To: adding elements in array to list in descending order using ListIterate

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

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 ?
Was This Post Helpful? 0
  • +
  • -

#3 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

Re: adding elements in array to list in descending order using ListIterate

Posted 21 October 2012 - 11:53 AM

View Postpbl, on 21 October 2012 - 11:46 AM, said:

What do you mean to compare from an array to a list to add in order ?


Like I have an array of Integers without any order at all. Then I want to add those elements in the array into LinkedList in descending order using ListIterator.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

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
Was This Post Helpful? 1
  • +
  • -

#5 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

Re: adding elements in array to list in descending order using ListIterate

Posted 21 October 2012 - 12:04 PM

View Postpbl, on 21 October 2012 - 11:56 AM, said:

The easiest way, that takes only a line of code, is to first sort the array


Arggggg I am so dumb, I kept thinking I will have to look and compare the values while adding elements at same time @_@
Was This Post Helpful? 0
  • +
  • -

#6 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

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....
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

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)
Was This Post Helpful? 0
  • +
  • -

#8 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

Re: adding elements in array to list in descending order using ListIterate

Posted 21 October 2012 - 01:24 PM

View Postpbl, 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)



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

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

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:

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;
    }
}


Was This Post Helpful? 0
  • +
  • -

#10 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

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?

This post has been edited by RozenKristal: 21 October 2012 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#11 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 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 :devil2:

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;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#12 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 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
Was This Post Helpful? 0
  • +
  • -

#13 RozenKristal  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 380
  • Joined: 29-September 12

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;
				}
			}
		}

Was This Post Helpful? 0
  • +
  • -

#14 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1