I am supposed to create an arraylist from an integer[] array, iterating the elements in the list in decending order

ex:

``````int[] array = {1,4,2,3,5};
goes into array list to become {5,4,3,2,1}
``````

how do i even being to do this (without first sorting the array and then putting it into the list?)

i am trying take an index from the array, check to see if it is smaller than the current number in the array, and bump it down into the ArrayList, but so far no good. any help would be appriciated

``````    int[] intArray = {1,9,4,5,3,2,8};
ArrayList<Integer> list = new ArrayList(1);

for(int i = 0; i < intArray.length; i++ )
{
if(i == 0){
}
//if i is not zero and intArray value at i is less than value at previous index
int[] intArray = {1,9,4,5,3,2,8};

//used to see what list looks like
ListIterator itr = list.listIterator();
while(itr.hasNext())
System.out.println(itr.next());
``````

edit

Hi. You could try using something like bubble sort in sorting your integer array.

java.util.Arrays.asList(intArray) will do fine incl. java.util.Collections.sort(..)
The 0 I would not put in the array or get that handled well where you're using the list, or make a function for that only

bryanvon
I dont think i can. as far as i know, I have to sort the array while inserting it into the arraylist

example:

``````int[] array = {1,4,2,3,5};

becomes (starting from the front of array)
list = {1};
list = {4,1};
list = {4,2,1};
list = {4,3,2,1};
list = {5,4,3,2,1};
``````

Is it a requirement to sort the array while inserting it into the arraylist? If not, I would suggest to put all the contents of your integer array into the arraylist first then sort them using Collections.sort() method.

You do not need to do any sorting before creating an ArrayList. You could simply read the data and try to insert the element at the correct location using add(index, E) to the array list.

``````intArray <- an integer array
arrayList <- an empty array list
for each element of intArray
aInt <- create an Integer object for the integer value
index <- get/search a proper index to be inserted into the arrayList
end for
``````

That's all. You don't need to do the sorting but implement a correct method to search/obtain a proper index to be inserted for the array list. The big O of this method should be O(n) too (iterate through each element is n, and the search should be a constant -- log2 -- because it is integer type).

``````  aInt <- create an Integer object for the integer value
index <- get/search a proper index to be inserted into the arrayList
``````

I am confused by these parts. what does creating an integer object do, and how does that correlate to inputting the value into the arrayList?
and this is the code you are talking about right?

``````Integer first = list.next();
``````

When you create an array list, you cannot use primitive data type to create.

``````ArrayList<int> array = new ArrayList<int>();  // <-- compile error!
``````

so you need a class and Integer is the closet to int

``````ArrayList<Integer> array = new ArrayList<Integer>();
``````

then you need it to be an integer in order to add it into the array list

``````Integer aInt = new Integer(an_integer_value);
``````

Now talking about index, it could be a portion of code or a method...

``````// if it is a method, it would look like...
int index = getInsertedIndex(arrayList, aInt);

// then you create a method
private int getInsertedIndex(ArrayList<Integer> arr, Integer val) {
// implement the search for a proper index inside the 'arr' list
...
}

// if it is not a method but a chunk of code...
int index=0;
... // same chunk of code from the getInsertedIndex() method here
...
``````

The simpliest algorithm to search for the index (big O(n)) is to go through each element in the array list and compare the value. In your case, if the value of val is greater than the value of the current iteration index in the list, return the current iteration index value (which is the correct inserted index). If you have gone through the whole list, return the current index you have already increased which should be equal to the length of the array list (add to the tail of the array list).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.