Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
can we resize an array in java? if not, explain this:
int arr[] = new int[1];
arr[0]=-10;
//arr[1]=1;
arr=new int[2]; //Explain this
arr[0]=-1;
arr[1]=1;
System.out.println(arr[0]+" "+arr[1]);
Resizing an array is impossible, as far as I'm concerned.
To understand why your code works, you need to understand that arrays are reference types. arr holds an reference to the actual array, like this:
holds points to
arr ----------> reference -----------> array object
In this line:
arr=new int[2];
You are not doing anything to the array object at the very end there. You're basically saying:
Hey arr. I don't want you to hold that reference anymore. Let go and hold this reference (which is an array with length 2)!
"What happens to the original array object with length 1 then?" you asked. This is where the GC comes into place. At some point, objects that has no reference pointing to, are collected.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Do Numbers inside such brackets { } always belong to arrays or can it be a primitive Type too?
The exact task is: Decide for the following values (!) whether Java provides primitive data types for their representation. If yes, specify all matching ones.
I'm only not sure about this one: {1,4,2}
There isn't any int [ ] infront of that, that's why i'm asking.
(sorry for the dumb question, very big noob here)
The code :
int[] array = {1,2,3};
Is equivalent to :
int[] array = new int[3];
array[0] = 1;
array[1] = 2;
array[2] = 3;
In java, arrays are objects (even if it's an array of primitive type)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
can we resize an array in java? if not, explain this:
int arr[] = new int[1];
arr[0]=-10;
//arr[1]=1;
arr=new int[2]; //Explain this
arr[0]=-1;
arr[1]=1;
System.out.println(arr[0]+" "+arr[1]);
Resizing an array is impossible, as far as I'm concerned.
To understand why your code works, you need to understand that arrays are reference types. arr holds an reference to the actual array, like this:
holds points to
arr ----------> reference -----------> array object
In this line:
arr=new int[2];
You are not doing anything to the array object at the very end there. You're basically saying:
Hey arr. I don't want you to hold that reference anymore. Let go and hold this reference (which is an array with length 2)!
"What happens to the original array object with length 1 then?" you asked. This is where the GC comes into place. At some point, objects that has no reference pointing to, are collected.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I wanted to use Dynamic Array in Java,
I will be declaring the array statically,where i will input values into the array ,once the array gets full i will reintialize the array size.
Whether the contents of the array will be removed or it will be maintained.
The contents are deleted in my program ,whether that is expected or not ?
Example
class a
{
static int arr[]=new int[10];
arr[]={1,2,3,4,5};
public static void main(String args[])
{
int N=35;
arr=new int[N];
arr[]={1....35};
}
}
Yes because when you think you're re-sizing it you're actually creating a new array with a different size. That is very much expected!!!
When you write:
arr=new int[N]
you actually discard your array object and create a new array of int of size N. This new object will not contain your previous array's values.
If you create a larger array, you must copy the previous values to it.
Better still, use one of Java's data structures such as Vector<Integer> or ArrayList<Integer>. These use dynamic arrays, but you don't have to know about it and shouldn't worry - they just work...
Better use arraylist, arraylist can grow which is not possible with arrays. if you to copy the contents into another array with different size use System.arraycopy() but previous array will exist. better way is using arraylist or vector
Arrays in Java are of fixed size. Use ArrayLists which is a collection & can dynamically scale.
Instead of
Integer[] ints = new Integer[x]
use
List<Integer> ints = new ArrayList<Integer>();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm making a method that takes 2 inputs, one is int[][], the other is int. I need to store that int value as the "row" of the int array. For example if I input (int[][] array, int 3493), I need the row to be {3 4 9 3}. This is basically like the 2048 games, as I need to be able to manipulate the array later. I do not know the proper syntax for this, however. Please help.
Thank you.
As An Sample, suppose x = new int[3][4], x[0], x[1], and x[2] are one-dimensional
arrays and each contains four elements, as shown in the figure x.length is 3, and
x[0].length, x[1].length, and x[2].length are 4
As you see you need two for loops to go through the array.
1. for traversing the row one by one
2. when you are in each row traversing each column one by one
for example:
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
When Java has no idea how big each element of the array is going to be, how much memory does it allocate?
Java always knows exactly how big each element of the array is going to be, because Java has only two kinds of arrays: arrays of primitives or arrays of object references.
If it's an array of primitives (int, long, etc.) it's the size of the primitive; and if it's an array of objects then it's the size of references to objects (pointers).