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>();
Related
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 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 want to make ArrayList of HashSet and use it.
**My Problem:**
I will get an Integer N as an user input.
I have to make a hashset and put some values in it.
Then add it to ArrayList and above step is to be done N times.
After it I have to check whether a element is present in the first/second/third/... or Nth hashset in ArrayList.
To check value in which hashset is also given by user.
I dont have to convert my ArrayList to hashset either I have to make ArrayList to hashset.
Just make an ArrayList of HashSets :
ArrayList<HashSet<Integer>> list = new ArrayList<HashSet<Integer>>();
Then create HashSets, fill them, and put them in your ArrayList normally.
HashSet<Integer> set = new HashSet<Integer>();
set.add(1);
set.add(whateverIntValue);
list.add(set);
You can then get the nth HashSet of your list using list.get(n).
List<Set<Integer>> sets = new ArrayList<>();
sets.add(someSet());
sets.add(someSet());
sets.add(someSet());
Set<Integer> someSet() {
Set<Integer> set = new Hashset<>();
Collections.addAll(set, 2, 3, 5, 7, 11);
return set;
}
This makes a distinction between specification / interface of variables an method parameters and results, and implementation / implementing class.
So you can change the implementation, say to a sorted TreeSet without pain.
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 7 years ago.
Improve this question
Suppose:
if I call one of my classes to my Main method, how many ways I can use the object ArrayList to make the list useful?
eg:
ArrayList <Account> listOfAccounts = new ArrayList<>();
Can I use listOfAccounts as Integer? if yes, how? or how to covert listOfAccounts as Integer ArrayList so that I can use them as in the method below?
class method:
int findMax(ArrayList <Integer> numberOfAccount){};
print in main method:
println("Maximum amount of money"+findMax(listOfAccounts));
Any help to clear the confusion will be appreciated. Thanks
If you just need to count the number of accounts you can do
int size = listOfAccounts.size();
if you want to extract a list of numbers from those accounts you can do
List<Integer> numberOfAccounts = listOfAccounts.stream()
.map(a -> a.getNum())
.collect(Collectors.toList());
how many ways I can use the object ArrayList to make the list useful?
Too many to count.
Can I use listOfAccounts as Integer?
Its a List of Accounts. If you want to use it as a list of Integers, you have to create a new List which contains the Integers you want.
how to covert listOfAccounts as Integer ArrayList so that I can use them as in the method below?
You can't convert the list, but you can get an integer out. I suggest you read the Javadoc for this class for more details.
int i = numberOfAccounts.get(i);
or
for(int i : numberOfAccounts) {
// iterate over the list
or use t as a Stream
numberOfAccounts.stream()
.forEach(System.out::println);
how to use findMaximum(listOfAccounts)? as listOfAccounts is object, not an specific type arrayList.
If you just want the maximum for some field of an Account, you don't need to extract a List<Integer> first.
List<Account> accounts = ...
OptionalInt max = accounts.stream()
.mapToInt(Account::getNum)
.max();
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 9 years ago.
Improve this question
I could use some help comparing two arrays, previously created in another method. They are called enterNumbers, user input array and drawNumbers, a randomly generated array.
My method header should look like this:
public static boolean containSameElements(int[] enterNumbers, int[] drawNumbers)
{
}
The method should compare the two arrays and return true if the numbers are the same, regardless of the order.
Not looking for the answer, just maybe a place to start.
Just sort them before
Arrays.sort(enterNumbers);
Arrays.sort(drawNumbers);
if(Arrays.equals(enterNumbers, drawNumbers)){
System.out.println("both are same");
}
Well, you can either
Create two histograms (using a hash based map/set) to count elements
in each array, and then compare the sets/maps. This solution is O(n)
space and O(n) time on average. Have a look at Map or Set for this. (Either you want Map or Set depends if existence of duplicates is important or not)
Another solution is sort and iterate. This is O(nlogn) time worst
case. Have a look on Arrays.sort() for this solution.
if (drawNumbers.length != enterNumbers.length)
return false;
List<Integer> base = new ArrayList<Integer>();
for (Integer i : drawNumbers)
base.add(i);
for (Integer i : enterNumbers)
base.remove(i);
return base.isEmpty();
This is a very common "problem" which can be solved using different methods. If I understand you correctly, all of the numbers have be inside the both arrays, but they don't have to be at the same indexes?
Then you can just make a while/for loop with (with two counters; one for each array) and check if the number on index 0 in the first array equals any of the numbers in the second array. If it doesn't the while/for-loop is done and the test failed. If it does go on to the next index in the first array. Continue until everything is tested(all numbers in first array versus the second array) or until a number doesn't exist in both arrays. Good luck