I want to have a array of integers where the length is variable. The obvious choice is to use ArrayList but can I do this for primitive types such as
ArrayList<int> myArray=new ArrayList<int>();
I dont want to use
ArrayList<Integer>
because the Integer class is clumsy in terms of coding.
EDIT: From the answers below I think the solution is to write my own Integer class.
To answer the question below about "clumsy" let me give a specific, and I would of thought common use for integers namely using the last member of the array in any place you would want the integer. If I just call the array "name" then to get the actual integer that can be operated on I need
name.get(name.size()-1).intValue();
To me this seems like an awfully unwieldy expression for a simple integer - particularly if it appears in an expression twice. It also seems that (most of the) methods available for the Integer class are absolutely redundant. Take two examples
static int compare(int a, int b)
Quite unbelievably, according to the documentation, this method returns a-b!!
static Integer valueOf(int a)
returns an Integer instance of the integer a. Can someone give me a single example where
new Integer(a)
does not achieve exactly the same result?
Method 1: (not recommended)
You can do something like this, but this doubles the code and is not efficient:
int[] a;
//get size (from command line maybe ow whatever method you want)
You can set size 0 initially, and for ex. you are transferring values from arraylist so you will have to write:
while(itr.hasNext()){
size++;} //itr is an object of Iterator
int i=0;
a=new int[size];
// then loop again to store values
while(itr.hasNext()){
a[i]=itr.next();
i++;}
Method 2:
Or you may use ArrayList without making it clumsy as follows:
ArrayList al=new ArrayList();
then you may declare Integer objects as volatile and perform operations on them just as you do with the primitive types.
Method 3: (not recommended)
Or simply write:
ArrayList al=new ArrayList();//ignore the warning about <E>
int x=2;
al.add(2);
Method 4: (recommended)
If I were you I would use ArrayList<Integer>.
UPDATE: Another thing that might work is that you may initially create an ArrayList<Integer> and store values there and later convert it to int[]
This SO answer tells about the conversion. Quoted the code form there:
public static int[] convertIntegers(List<Integer> integers)
{
int[] ret = new int[integers.size()];
for (int i=0; i < ret.length; i++)
{
ret[i] = integers.get(i).intValue();
}
return ret;
}
Hope this helps.
No it's not possible to use primitive types as generic type.
Well I would recommend you do use ArrayList and avoid primitive types in this case.
You can't change the size of an array once created. You have to allocate it bigger than you think you'll ever need
or
Accept the overhead of having to reallocate it to a new larger array and copy the data from the old to the new:
System.arraycopy(oldItems, 0, newItems, 0, 10);
But Much simpler to go with ArrayList.
Related
An interview question was to write this method to remove duplicate element in an array.
public static Array removeDuplicates(Array a) {
...
return type is java.lang.reflect.Array and parameter is also java.lang.reflect.Array type.
How would this method be called for any array?
Also not sure about my implementation:
public static Array removeDuplicates(Array a)
{
int end=Array.getLength(a)-1;
for(int i=0;i<=end-1;i++)
{
for(int j=i+1;j<=end;j++)
{
if(Array.get(a, i)==Array.get(a, j))
{
Array.set(a, j, Array.get(a, end));
end--;
j--;
}
}
}
Array b=(Array) Array.newInstance(a.getClass(), end+1);
for(int i=0;i<=end;i++)
Array.set(a, i, Array.get(a, i));
return b;
}
You may want to consider using a different data structure such as a hashmap to detect the duplicate (O(1)) instead of looping with nested for loops (O(n^2)). It should give you much better time complexity.
There are various problem with this code. Starting here:
if(Array.get(a, i)==Array.get(a, j))
Keep in mind that those get() calls return Object. So, when you pass in an array of strings, comparing with == simply will most likely result in wrong results (because many objects that are in fact equal still have different references --- so your check returns false all the time!)
So, the first thing to change: use equals() instead of == !
The other problem is:
end--;
Seriously: you never ever change the variable that controls your for loop.
Instead: have another counter, like
int numberOfOutgoingItems = end;
and then decrease that counter!
For your final question - check the javadoc; for example for get(). That reads get(Object array, int index)
So you should be able to do something like:
int a[] = ...;
Object oneValue = Array.get(a, 0);
for example.
Disclaimer. I have to admit: I don't know if the Array implementation is smart enough to automatically turn the elements of an int[] into an Integer object.
It could well be that you have to write code first to detect the exact type of array (if it is an array of int for example); to instead call getInt() instead of getObject().
Beyond that, some further reading how to use reflection/Array can be found here
I want a 2D Matrix with one line of strings and the other line with int's.
Is that possible?
Or do I have to save the int's as strings and later convert them to int's again?
Rather use an object.
class MyEntry {
String foo;
int number;
}
MyEntry[] array = new MyEntry[10];
But if you must, you can use two types - only through an Object supertype.
Object[][] arr = new Object[2][10];
arr[0][0] = "Foo";
arr[1][0] = new Integer(50);
No it is not possible . There can be only a single datatype for an array object. You can make a class having both the int and String as property and use it. Never use an Object[][] even if there is a temptation to do so, it is an evil workaround and hacks fail more than they succeeded . If Object was a sound technique then they wouldn't have introduced Generics for Collection !
You can create Objects 2D array and place there Strings and Integers, but I am not sure if it is good idea to have mixed types in arrays. You should probably describe your problem more so we could figure out better way.
Yes it is. If you declare as a Matrix of object then you can store string and Integer (not int), the difficulty will be after to retrieve them correctly :)
You can create an array of the type Object and store any non-primitive Object in there.
When you retrieve them, you'll need to make sure you check their class though.
if(objArray[0] instanceof String) {
// do string stuff
} else if(objArray[0] instanceof Integer) {
// do integer stuff
}
etc.
I think you're better off creating a new class that can store objects of the types that you want and just retrieve them using getters and setters. It's a lot safer and more stable.
You could do it if you do a 2D array of Object as in Object[][] myArray = new Object[x][y] where x and y are numbers.
All you would have to do is cast the Objects to their expected types before using them. Like (String) myArray[0][3] for example.
YOu should only do it this way if you know for certain what type the Object in a particular location will be.
However, it's generally not a good idea to do things this way. A better solution would be to define your own data structure class that has a String array and an int array as member variables. As in:
public class myData {
String[] theStringArray;
int[] theIntArray;
public myData(String[] sArraySize, int[] iArraySize) {
this.theStringArray = new String[sArraySize];
this.theIntArray = new int[iArraySize);
}
...
// Additional getters / setters etc...
...
}
i have an array of strings which i want to convert to int, pretty simple and straightforward here is the code :
public static void main(String[] args) {
String myarray[]=readfile("[pathtothefile]");
int mynums[] = new int[myarray.length];
for (int i=0;i<myarray.length;i++){
mynums[i]=Integer.parseInt(myarray[i]);
}
System.out.print(Arrays.toString(mynums));
}
But the Problem here is, if i initialize "mynums" like this: mynums[]=null; i get NullPointerException on the following line:
"mynums[i]=Integer.parseInt(myarray[i]);"
what i have to do to solve it is
int mynums[] = new int[myarray.length];
here someone explained why it happens but i dont know how to initialize now! i mean sometimes i dont know how big my array can get and i just want to initialize it. is it even possible?
In Java everything is a pointer behind the scenes. So when you do mynums[]=null, you are pointing to a null. So what is null[i]? That is where your NPE comes from. Alternatively when you point it to an array, then you are actually accessing the i'th element of the array.
You have to first initialize the array because it allocates memory depending on the array size. When you want to add for example an integer to an array it writes the int into previously allocated memory.
The memory size won't grow bigger as you add more items.( Unless you use Lists or Hashmaps, ... but it's not true for generic arrays)
If you don't know how big your array will be, consider using SparseIntArray. which is like Lists and will grow bigger as you add items.
Briefly, in java an array is an object, thus you need to treat it like an object and initialize it prior to doing anything with it.
Here's an idea. When you're initializing something as null, you're simply declaring that it exists. For example ... if I told you that there is a dog, but I told you nothing about it ... I didn't tell you where it was, how tall it was, how old, male/female, etc ... I told you none of its properties or how to access it, and all I told you was that there IS a dog (whose name is Array, for sake of argument), then that would be all you know. There's a dog whose name is Array and that is it.
Typically, arrays are used when the size is already known and generally the data is meant to be immutable. For data that are meant to be changed, you should use things like ArrayList. These are intended to be changed at will; you can add/remove elements at a whim. For more information about ArrayList, read up on the links posted above.
Now, as for your code:
public static void main(String[] args) {
ArrayList<int> myInts = new ArrayList<int>();
// define a new null arraylist of integers.
// I'm going to assume that readfile() is a way for you get the file
// into myarray. I'm not quite sure why you would need the [], but I'll
// leave it.
String myarray[] = readfile("[pathtothefile]");
for (int i = 0; i < myarray.length; i++) {
//adds the value you've specifed as an integer to the arraylist.
myInts.add(Integer.parseInt(myarray[i]));
}
for (int i = 0; i < myInts.size(); i++) {
//print the integers
System.out.print(Integer.toString(myInts.get(i)));
}
}
What if you don't use an array but an ArrayList? It grows dynamically as you add elements.
Is using an arraylist of Tuple(double,int,int) slower than three separate arraylists? I want to avoid creating lots of Tuple objects, but does method 2 create objects by autoboxing?
//Method 1
Arraylist<Tuple> arr=new Arraylist<Tuple>();
Tuple t=new Tuple(double, int, int);
class Tuple{
private double value;
private int a;
private int b;
}
//Method 2
Arraylist<Double> arr=new Arraylist<Double>();
Arraylist<Integer> arr=new Arraylist<Integer>();
Arraylist<Integer> arr=new Arraylist<Integer>();
Your question is missing context. This problem has been asked many times, and there is no single best solution.
In my opinion, the best way to model the data is to have a logical type that represents your data. (You are currently using a tuple, but it would be better to have a specific type with methods.)
So, I would do the following:
List<NumberContainer> list = new ArrayList<NumberContainer>();
As far as speed goes in particular - It depends on how you are going to use the data. If you are looking for fast access times, it may be best to use a map and key each item on some value.
Unless you've written a custom Tuple class which maintain an unboxed double and two int values, they'll be boxed anyway... so basically you'll end up with the extra Tuple object per item, although just one underlying array and ArrayList instead of 3.
If the triple of values represents a meaningful composite value though, I'd be very tempted to write a small class to encapsulate the three of them with meaningful names for each property. That way you're likely to end up with more readable code and efficient code (as there won't be any boxing).
Most likely using an array of objects (or in your case, tuples) which would save you a line of code, and put everything in one place (the tuple.)
Here's the sample code for what I would do.
//Class
class container() {
int value1, value2;
double value3;
//Constructor
container(int value1, int value2, double value3) {
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
}
}
//Implementation
ArrayList<container> arr=new ArrayList<container>();
If Tople is a class with 3 ivars, in that case that would be the way to go.
Aditionaly arralist only take objects so it will autobox all the primitives, but if you are using a class it will definitly not autobox the ivars in the class.
To answer your direct question, method2 does create objects by autoboxing assuming that the values you're putting in are primitives (double, int, etc.). Of course, if you use a Tuple class, you're also creating objects, but you will be creating 1/3 the number of objects, assuming the Tuple class maintains two ints and a double.
Is there some hidden meaning in this code which I don't see in java? How can it be useful?
int[] a = new int[1];
than just
int a;
because from my point of view it's the same?
int a
defines a primitive int.
int[] a = new int[1];
defines an array that has space to hold 1 int.
They are two very different things. The primitive has no methods/properites on it, but an array has properties on it (length), and methods (specifically its on clone method, and all the methods of Object).
Arrays are a bit of a weird beast. They are defined in the JLS.
In practice, it would make sense to do this when you need to interact with an API that takes an array and operates on the results. It is perfectly valid to pass in a reference to an array with 0, 1, or n properties. There are probably other valid reasons to define an array with 1 element.
I can't think of any use cases where you would want to define an array with one element, just to bypass the array and get the element.
One is on the stack, one is on the heap.
One difference is that you can write a method that changes its int argument by changing arg[0]. This trick is used quite a bit in some of the code I've seen. It allows you to, for instance, return a boolean indicate success or failure and an int value that serves some other purpose. Without that trick, you'd have to return some sort of object containing the two values.
int a;
defines a variable that can hold an int
int[] a;
defines a variable that can hold an array of int
int[] a = new int[1];
does that above but also initializes it by actually creating an array (of size 1 - it can hold 1 int) and defines the variable a to hold that array, but doesn't define what's in the array.
int[] a = new int[1]{1};
does that above but also defines what's in the array: the int 1.
I suppose it does a similar thing, in that space is allocated for 1 int, but the array also defines an array. I suppose you could say these are similar:
int a = 1;
int b = a + 1;
// now b == 2
int[] a = new int[1]{1};
int b = a[0] + 1;
// now b == 2
An array of size one is not the same thing as a single integer.
Even if they carry the same information, they are different types, so you can use them in different contexts.
For example, if you have a function which performs a function on all elements of an array but you want to compute it only on one value, you should pass a int[1], because the function expects an array and wants to know how many values it should process.
All arrays in java are objects. when declaring: int x = 5; you're declaring a primitive type.
When declaring int[] x = new int[]; you're creating an object with type int[].
So int[] is actually a class.