Basic array initialization and sorting question - java

this is a rather basic java question
I have an array containing String that i want to sort using java.util.Arrays.sort
when i write
String[] myArray = {"A","B","C"};
java.util.Arrays.sort(myArray);
it gets sorted correctly
however when i have
String[] myArray = new String[10];
myArray[0] = "A";
myArray[1] = "B";
myArray[2] = "C";
java.util.Arrays.sort(myArray);
sort throws a nullreferenceexception
i'm pretty sure its something really dumb i just dont get right now. I have to new the String, because hardcoding default values doesnt get anyone, anywhere.

When you initialize the second array, you only initialize the first three elements. The other elements are initialized to null and thus can't be sorted.

In the source, the method uses compareTo() as a sort condition. Obviously, invoking compareTo() on null, will raise a NullPointerException. As its mentioned in Java Docs that,
All elements in the array must be
mutually comparable (that is,
e1.compareTo(e2) must not throw a
ClassCastException for any elements
e1 and e2 in the array)
Of course here its not about ClassCastException, but invocation of comapreTo() is obvious.
[Edited]
P.S. Figuring this out from Exception Stack Trace is your best bet.

try the following to sort just the first three elements.
Arrays.sort(myArray, 0, 3);

What happens is that when you have the array that has ten items, the other items are uninitialized. So when the sort algorithm works, it tries to take the value of the string, but instead gets nothing, and throws the exception.
You need to make sure that you don't try to sort an array that has more space than things you place into it.

I think that since you make myArray store 10 elements, it's (effectively) adding 7 nulls to the end, and then Arrays.sort() can't sort a null element against a string.

It cant work on null strings which are there when you initially create the Array.
To avoid either explicitly make all as "" or else assign as much as require.
Although dont know whether this is a miss at API level as they could have catered for the null object (the way we get in SQL orderby) or is there something more to it.

You defining the array size by 10 but only initializing 3 indexes. Change your array size to 3 or initialize all ten.

The array has 10 items, but you only put 3 inside. So, the other 7 are NULL. Consider using a ArrayList instead.

An array list should be used because there are 3 elements not 10.
The other 7 elements of the array will have a null value.
It is these null values that cause the Null Pointer issue when sorting.

Related

How to initialize an ArrayList with a certain size and directly access its elements

I was writing something that needs an arrayList of size n, so I did the following:
List<Set<Integer>> list = new ArrayList<Set<Integer>(n);
And when I was trying to access an element of the list e.g.
list.get(someValue that is <n)
I got arrayList out of bound exception, so I guess putting a n there doesn't really help you initialize the list, but just pre-allocate the space.
Is there a way to do the initialization after which there are actually null or objects in each slot?
I end up using a for loop and adding n empty set and then index into the list.
Is there a better way TO INITIALIZE AN ARRAYLIST IF THE SIZE IS KNOWN IN ADVANCE?
Please know what I'm asking before saying this is a duplicate.
Hope my question is clear.
Thanks in advance.
Some of you think what I tried to do is meaningless. This happens when I tried to solve a bucket sort related problem where the index of the set I tried to access in the array is known. So for example, I want to add some elements to the set at position 1, 3, 2, 4... then it would be convenient if I can just get the set at position 1, 3, 2, 4...
If you take a look at Java API documentation, you will see that there are 3 constructors provided for ArrayList:
ArrayList()
ArrayList(Collection c) - Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
ArrayList(int initialCapacity) - Constructs an empty list with the specified initial capacity.
You can use second of listed constructors if you need to fill ArrayList's slots in the place of its definition. You need to be aware that there needs to be passed a Collection, so you can do something like that, for example:
ArrayList<Integer> al = new ArrayList<>(Arrays.asList(1,2,3,4,5);
Now size() of al is 5 and it is filled with numbers 1,2,3,4 and 5.
Also, you need to be aware that ArrayList doesn't work like Arrays and it's size is not fixed - it changes due to adding or removing items.
I'm taking the liberty of repeating bcsb1001's solution, which is perhaps obscured by various comments. In order to initialize an array of a fixed size with valid placeholder values:
new ArrayList (Collections.nCopies (n, null));
This idea was the best I could find in various searches, maybe it will work for others too.

Is there a way to instantiate a comparable array, just establishing its length

I need to "extract" if you will, one particular row of a multi array of comparables. I know the index of the row, I just need a copy of it. I tried just doing something like:
masterList is the multiarray that I need to extract from.
Comparable[] extracted = masterList[i];
This however only sets extracted to the address and not the actual contents.
Is there an easy way to do this?
If not can I create an empty Comparable[] of the same length as the masterList[i] row and loop through to add to it? Please note that my program doesn't start off knowing the length of the row it needs so I can't hard code it into the program.
Updated code:
Comparable[][] comparable = Arrays.copyOf(masterList, masterList[i].length);
When I ran this I did get an array of the length I need but it was all addresses, not values. So I ran a loop to add the values
for(int i = 0; i<comparable.length; ++i){
comparable[i] = queries[masterIndex];
}
this however still returns a list of addresses.
If not can I create an empty Comparable[] of the same length as the masterList[i] row and loop through to add to it?
No need. You can just do
Comparable[] extracted = Arrays.copyOf(masterList[i], masterList[i].length);
Please note that my program doesn't start off knowing the length of the row it needs so I can't hard code it into the program.
See, you need not to hard code the length as well.
Have you tried System.arraycopy?
Comparable extracted[] = new Comparable[masterList[i].length]
System.arraycopy(masterList[i],0, extracted, 0, extracted.length);
This should make a shallow copy of the elements at masterList[i][j=0 to length-1] in extracted.
Please note that both of the solutions described here will throw a NullPointerException if master list[i] happens to be null. Be sure to check for that to avoid a nasty runtime surprise.
System.arraycopy is handy if you want to copy a part of the array. Here is a good discussion on both the approaches.
Hope this helps!
You can use clone(), because all arrays are Cloneable:
Comparable[] extracted = masterList[i].clone();
This will create a shallow copy of masterList[i] (if masterList[i] is null, you'll get a NullPointerException).

Solving CodingBat sum28 and withoutTen with array lists

So I just finished some problems off of codingbat.com, sum 28 and withoutTen, and I wanted to know if there was a way to do them with array lists rather than just arrays. I am trying to get some practice with array lists before my next coding assignment.
So, if your question is "can I use array lists instead of the arrays on codingbat?", the answer is yes.
Just a couple of small points change.
With arrays, you'd use array.length to get the size. For arraylists, it is arraylist.size()
To access elements in an array, you use array[0]. For an arraylist, it is arrayList.get(0).
Finally, it initialize an array, you use int[] array = new int[10];. For an arraylist, you'd use ArrayList<Intenger> name = new ArrayList<Integer>();. You typically won't assign it a size. To add elements to it, use name.add(5).
Just make sure to add import java.util.ArrayList; at the top of your class so that you can actually access all of these methods.
I hope that helps. Good luck :)

Behavior of an array after sorting in Java

I want to get some views on the behavior of the following program :
package main;
import java.util.Arrays;
public class StringAnagram {
public static void main(String args[]) {
String a = "aabbaabb";
char[] aArr = a.toCharArray();
Arrays.sort(aArr); //1
Arrays.sort(a.toCharArray()); //2
System.out.println(aArr); // Sorted
System.out.println(a.toCharArray()); // UnSorted
}
}
According to me statement 1 sorts the character array referenced by aArr but when it comes to statement 2 the sorting of character array is taking place but somehow the sorted array is not referenced by any variable, so the behavior is lost. Could someone please help me with the same.
Yes. You are right.
Each call to toCharArray() actually creates a new array instance with the characters in the string.
In the case of aArr, you actually refer to the new array instance, you use aArr to sort. Its the array instances referred by the variable aArr which gets sorted.
But when you pass a.toCharArray() to Array.sort() method, you are passing array instance which you don't have a variable referring to. The array instance gets sorted but you don't have any reference.
When you you call println using a.toCharArray() again a new array instance is created and passed to println which is obviously unsorted.
Well, let's see what happens.
First, a.toCharArray() is called. This returns a new char array containing the chars "aabbaabb".
Then, this array is sorted.
You didn't give yourself a way to access the array, so you can't. This is what "lost" means here. Nothing special or magic happened, you just made an array you can't access. It's not going to waste memory - the garbage collector will detect that it can't be accessed, and destroy it.
It's similar to if you just did this without the sorting:
a.toCharArray();
Again, toCharArray goes and makes an array for you, and you don't give yourself a way to use it, so it's also "lost". The sorting was a red herring.
Arrays.sort(a.toCharArray());
This sorts the array returned by a.toCharArray() and not the String a. So whenever you do a.toCharArray() you get a separate(new) Character array of the String. Hence it is unsorted.

Using an array of custom objects

Let's say I've created an array of objects SpecialResource via
ArrayList<SpecialResource> masterRes = new ArrayList<SpecialResource>();
masterRes.add(0, new SpecialResource(3,5,0,"Foo Bar"));
.........etc etc many more adds...
Let's say SpecialResource has a method calledgetMax()
How would I reference the getMax method of array index 0? Every permutation I've guessed at is giving syntax errors. masterRes<0>.getMax(), masterRes(0).getMax(), etc.
Well, actually, it's not an array, but a collection. And, in order to retrieve its items by index, you must use the get method:
masterRes.get(0).getMax();
masterRes.get(0).getMax();
Java/Android API document will help you.
http://androidappdocs.appspot.com/reference/java/util/ArrayList.html

Categories

Resources