I am using the following way to put int values from a ResultSet into an int array. This seems very innefficient but I can't figure out how to get an array of primitive int's where I don't know the size beforehand.
List<Integer> ints = ArrayList<Integer>();
while ( results.next ) ints.add( results.getInt( "id" );
int[] intsArray = new int[ ints.size() ];
for ( int i = 0; i < ints.length; i++ ) int[ i ] = ints.get( i ); //auto-boxes here
I need these to be a primitive array as that's what a method requires.
If you don't know the number of values read from the result set you really can't avoid using a list.
But you can avoid using a List<Integer> if you have a list class which can store primitive int values like for example TIntList of the trove project.
In the end you should really measure the performance impact of using intermediate Integer objects before spending too much energy on that question.
Convert ArrayList to array
You would have to guess an initial size and then resize the array as you fill its last element, repeating as needed as you go through your result set.
This behavior is exactly what ArrayList already gives you. But already debugged. ;-)
So just use ArrayList<Integer>. Convert to an array at the end.
You might think to use Collection::toArray. But that utility can only return an array of objects, not primitives.
See this Question, for several good ways to convert to array of primitives.
My choice would be calling on Google Guava. While adding Guava for this one purpose might be overkill, Guava is so useful so often that I add the library by default to most any new Java project.
List< Integer > list = ... ;
int[] values = Ints.toArray( list ) ;
If your deployment environment were extremely tight on memory, or if you had a very large number of elements, or if you were doing this operation so often as to impact performance, only then would I resort to filling-and-resizing an array int manually yourself.
Related
I am sure that this post silly, while I need still some practical ideas. I have 20 double[] arrays named like colVal1, colVal2,... Now I have 5 variables say que1, que2, que3, que4, que5 which contain integers from 1 to 20. I want to use arrays depending on the value contained in quei. Means if que1 contains 3 then I like to use colval3 in calculation. Manual use is avoidable due permutations of 20 numbers in 5 variables. Google hints that Java, in principle avoid replacement of variable name by another variable. I am lost at using HashMap. I could not use Reflection APIs correctly due to my limited knowledge base. Any handle is welcome.
Thanks and Regards
You know how to use an array I assume, so use an array of arrays.
double[][] colVal = new double[21][];
for (int i = 1; i <= 20; i++) {
double[] row = colVal[i];
System.out.println(Arrays.toString(row));
}
Note: this might be simpler if you started at 0 for the first row, instead of 1. i.e. you wouldn't need the unused row at the start.
I know that there were other questions related to this topic.
But this is a little different.
Immagine that you have a program that gets every some amount of time an 2d Object (Object[][]) from the excel and it pass this to one method; this method needs to concatenate this 2d Object, so when it gets a flag that there is no more input, it passes this potentially big 2d object as a result to some other method...
1) You don't know how many excel documents will be sent , so how many 2d object will you get
2) every 2d object can have different num of columns and rows (ok this is not a big problem)
Is there a way in java to concatenate 2d objects into one? Wihout "killing" the memory.
Looking at this accepted anwer
How do you append two 2D array in java properly?
there is an append method, but each time it is called (and in this described case we don't know how many times could be called), it recreates an 2d array, and in my opition is not a best practice for this solution.
Looking at this accepted solution How to concatenate two-dimensional arrays in Java there is used an arraycopy solution, which how i understood is something similar to the previous solution... maybe a little better.
I know that would be possible to use some lists that could somehow rappresents this 2d object, or be parsed at the end in the 2d object, but:
is there any way in java (good practice) to concatenate dinamically unown number of 2d object into 1?
tnx
It look like you need ArrayList< ArrayList < Object > > instead of Object[][]. If you use ArrayList, the different num of columns and rows aren't problems at all, and you not "killing" the memory if you use addAll method. Moreover, you could use a core Java methods to concat ot ArrayLists without create your own manual methods.
For example, you can use something like this:
public List<ArrayList<Object>> concat (List<ArrayList<Object>> list1, List<ArrayList<Object>> list2) {
if(list1.size() > list2.size()) {
List<ArrayList<Object>> listBig = new ArrayList(list1); // or just list1 if we can change list1
List<ArrayList<Object>> listSmall = list2;
} else {
List<ArrayList<Object>> listBig = new ArrayList(list2); // or just list1 if we can change list1
List<ArrayList<Object>> listSmall = list1;
}
for(int i = 0; i < listSmall.size(); i++) {
listBig[i].addAll(listSmall[i]);
}
return listBig;
}
I want to know how to add or append a new element to the end of an array. Is any simple way to add the element at the end? I know how to use a StringBuffer but I don't know how to use it to add an element in an array. I prefer it without an ArrayList or list. I wonder if the StringBuffer will work on integers.
You can not add an element to an array, since arrays, in Java, are fixed-length. However, you could build a new array from the existing one using Arrays.copyOf(array, size) :
public static void main(String[] args) {
int[] array = new int[] {1, 2, 3};
System.out.println(Arrays.toString(array));
array = Arrays.copyOf(array, array.length + 1); //create new array from old array and allocate one more element
array[array.length - 1] = 4;
System.out.println(Arrays.toString(array));
}
I would still recommend to drop working with an array and use a List.
Arrays in Java have a fixed length that cannot be changed. So Java provides classes that allow you to maintain lists of variable length.
Generally, there is the List<T> interface, which represents a list of instances of the class T. The easiest and most widely used implementation is the ArrayList. Here is an example:
List<String> words = new ArrayList<String>();
words.add("Hello");
words.add("World");
words.add("!");
List.add() simply appends an element to the list and you can get the size of a list using List.size().
To clarify the terminology right: arrays are fixed length structures (and the length of an existing cannot be altered) the expression add at the end is meaningless (by itself).
What you can do is create a new array one element larger and fill in the new element in the last slot:
public static int[] append(int[] array, int value) {
int[] result = Arrays.copyOf(array, array.length + 1);
result[result.length - 1] = value;
return result;
}
This quickly gets inefficient, as each time append is called a new array is created and the old array contents is copied over.
One way to drastically reduce the overhead is to create a larger array and keep track of up to which index it is actually filled. Adding an element becomes as simple a filling the next index and incrementing the index. If the array fills up completely, a new array is created with more free space.
And guess what ArrayList does: exactly that. So when a dynamically sized array is needed, ArrayList is a good choice. Don't reinvent the wheel.
The OP says, for unknown reasons, "I prefer it without an arraylist or list."
If the type you are referring to is a primitive (you mention integers, but you don't say if you mean int or Integer), then you can use one of the NIO Buffer classes like java.nio.IntBuffer. These act a lot like StringBuffer does - they act as buffers for a list of the primitive type (buffers exist for all the primitives but not for Objects), and you can wrap a buffer around an array and/or extract an array from a buffer.
Note that the javadocs say, "The capacity of a buffer is never negative and never changes." It's still just a wrapper around an array, but one that's nicer to work with. The only way to effectively expand a buffer is to allocate() a larger one and use put() to dump the old buffer into the new one.
If it's not a primitive, you should probably just use List, or come up with a compelling reason why you can't or won't, and maybe somebody will help you work around it.
As many others pointed out if you are trying to add a new element at the end of list then something like, array[array.length-1]=x; should do. But this will replace the existing element.
For something like continuous addition to the array. You can keep track of the index and go on adding elements till you reach end and have the function that does the addition return you the next index, which in turn will tell you how many more elements can fit in the array.
Of course in both the cases the size of array will be predefined. Vector can be your other option since you do not want arraylist, which will allow you all the same features and functions and additionally will take care of incrementing the size.
Coming to the part where you want StringBuffer to array. I believe what you are looking for is the getChars(int srcBegin, int srcEnd,char[] dst,int dstBegin) method. Look into it that might solve your doubts. Again I would like to point out that after managing to get an array out of it, you can still only replace the last existing element(character in this case).
one-liner with streams
Stream.concat(Arrays.stream( array ), Stream.of( newElement )).toArray();
I am making an algorithm to save a path of zeroes between 0 1 array
the length of the path will be variable and so i need an array without predefined length
You cannot use array without specifying its length. Consider using ArrayList instead.
You can use an ArrayList which can be between 0 and around 2 billion in lengths.
If you use using a values or 0 and 1, a BitSet may be more efficient.
You can use:
ArrayList<Integer> al = new ArrayList<Integer>();
Note if you wanted to sort this array (for example in ascending order) - you would NOT use
Arrays.Sort(al);
You WOULD use:
Collections.Sort(al);
Use ArrayList instead. An ordinary array cannot be resized so easily - you would have to create a new one of a bigger size and copy the old one into it - I would not recommend this.
ArrayList al = new ArrayList();
al.add(0);
al.add(1);
You can always create array dynamically, e.g. new int[n] where n contains the array length at this moment (not pre-defined at compile time).
But array size cannot be changed. If you need this you should use List instead:
List<Integer> list = new ArrayList<Integer>();
Now you can add and remove elements when you need and the list size will be changed dynamically: you do not have to care about it:
list.add(123);
list.remove(456);
Use ArrayList for a dynamic size.
you can use ArrayList<Boolean> or ArrayList<Integer> and just use the add method. Then utilise the utility methods to get an array out when you are finished constructing the path.
String myArray[] = new Sting[YourVariable];
If speed is a real issue... and you need a dynamically changing array, you could try the following:
// this only works for increasing array-size
int[] tmp = new int[new_bigger_size]; // create a new array
System.arraycopy(array, 0, tmp, 0, array.length); // copy the data
int nextPosition = array.length
array = tmp; // assign the reference
// from position array.length (nextPosition), the new elements can be copied,
// for example:
array[nextPosition] = 120;
....
NOTE! This is very C-ish and not ideal. It is also more difficult to maintain uses more memory during the resizing and is regarded as bad form. Only try this as a last resort and if an ArrayList is really not working for you in terms of speed.
Having said that, does someone have an idea of how much slower (if any) an ArrayList will be?
I need to sort an array based on the positions held in another array.
What I have works, but it is kinda slow, is there a faster/better way to implement this?
2 Parts:
Part1
int i = mArrayName.size();
int temp = 0;
for(int j=0;j<i;j++){
temp = mArrayPosition.get(j);
mArrayName.set(temp, mArrayNameOriginal.get(j));
}
In this part, mArrayPosition is the position I would like the mArrayName to be in.
Ex.
input:
mArrayName= (one, two, three)
mArrayPosition = (2,0,1)
output:
mArrayName= (three, one two)
Part 2
int k=0;
int j=0;
do{
if(mArrayName.get(k)!=mArrayNameOriginal.get(j)){
j++;
}else{
mArrayIdNewOrder.set(k, mArrayId.get(j));
k++;
j=0;
}
}while(k < mArrayName.size());
}
In this part, mArrayName is the reordered name array, mArrayNameOriginal is the original name array.
Ex.
mArrayName = (three, one, two)
mArrayNameOriginal = (one, two, three)
Now I want to compare these two arrays, find which entries are equal and relate that to a new array that has their rowId number in it.
Ex.
input:
mArrayId = (001,002,003)
output:
mArrayIdNewOrder = (003,001,002)
So then I will have mArrayIdNewOrder id's matching up with the correct names in mArrayName.
Like I said these methods work, but is there a faster/better way to do it? I tried looking at Arrays.sort and comparators but they only seem to sort alphabetically or numerically. I saw something like I can create my own rules inside the comparator but it would probably end up being similar to what I already have.
Sorry for the confusing question. I'll try to clear up any ambiguities if needed.
The best performance read I've found is Android's Designing For Performance doc. You are violating a couple of the "Android way" style of doing things that will help you.
You are using multiple internal getters inside each loop for what looks like a simple value. Redo this by accessing the fields directly.
For extra credit, post your performance comparison results! I'd love to see em!
You could use some form of tuple, some class to hold both id and name. You'll just to have a java.util.Comparator that compares it accordingly, both elements will move together and your code will be cleaner.
This data structure might be convenient for the rest of your program... if not, just take things off it again and you're done.
If your order indexes are compact, i.e. from index 0 to size - 1, then just use an array and create the updated list afterwards? About something like
MyArray[] array = new MyArray[size];
for(int j=0;j< size;j++) {
array[ mArrayPosition.get(j) ] = mArrayName.get(j);
}
// create ArrayList from array