• Post Reply Bookmark Topic Watch Topic
  • New Topic

Increasing the capacity of the ArrayList  RSS feed

 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI, Everyone!! So could someone to help me with a method?
I have a method called double Capacity and inside of it, I should increase the capacity of my ArrayList which I am using.

Here I've attached the method which I am struggling with


 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "^" operator is the XOR operator -- it does an exclusive OR of the bits. So, with "students.size()^2", if the size was 16, then the new size would be 18. And if the size was 18, then the new size would be 16.  I don't think that this is what you want in "increasing the capacity" -- especially, depending on the size, it may even result in a new array those capacity is smaller.

Henry
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I understand that with this "^" operator it won't increase the capacity but how is the method for increasing the capacity.

I am working with ArrayList not with Array and that's why I am having torubles ....

so could you help me with this?
 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:Ok I understand that with this "^" operator it won't increase the capacity but how is the method for increasing the capacity.

I am working with ArrayList not with Array and that's why I am having torubles ....

so could you help me with this?


Use arraylist instead of array .Arraylist are dynamic in nature and i resize itself automatically

Syntax ArrayList<Type> arraylistname=new ArrayList<Type>();

In your case type=student
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:
I am working with ArrayList not with Array and that's why I am having torubles ....


Not sure what you mean by this. Do you mean that you are writing your own ArrayList class? Or are you using the ArrayList class that is part of the core Java library?

If you are writing you own ArrayList class, then how are you going to do it without arrays?

And if you are using the ArrayList class in the core Java library, then what is wrong with using the ensureCapacity() method?

Henry
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok but I already working with ArrayList

I've replied you before this that I am working with ArrayList

I made this method similar to the increasing the length of the Array that's why maybe I doing something wrong

so how to increase the size of my ArrayList, could you give me some help?
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is a part of my class which I am practising for the coming exam

 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it gives me error at this line :


 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:So this is a part of my class which I am practising for the coming exam


Yeah, none of that is going to work. The ArrayList (from the core Java library) does not give you access to the underlying array, nor does it give you full control of the capacity.

The ArrayList automatic checks the capacity, and increases it if needed -- so, you won't have to do anything regarding the capacity. However, even if you want to control the capacity, the best you can do is make a request to ensure the capacity.

Henry
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so as you can see in the picture I should implement all methods from the UML

and the last one is the doubleCapacity

that's why I am asking for help
uML.jpg
[Thumbnail for uML.jpg]
 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we try to add an object to the arraylist,

Java checks to ensure that there is enough capacity in the existing array to hold the new object. If not, a new array of a greater size is created, the old array is copied to new array using Arrays.copyOf and the new array is assigned to the existing array.

Look at the code below (taken from Java ArrayList Code at GrepCode.com).

Check this example


public ArrayList() Constructs an empty list with an initial capacity of ten.

public ArrayList(int initialCapacity) we can specify initial capacity.

public ArrayList(Collection c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Now when we use ArrayList() constructore we get a ArrayList with Size=10 On adding 11th element in the list new Arraylist is created inside ensureCapacity() method.

Using following formula:

  int newCapacity= (oldCapacity * 3)/2 +1;

 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:it gives me error at this line :




what type of error ?
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Type mismatch: cannot convert from Student[] to ArrayList<Student>



 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:so as you can see in the picture I should implement all methods from the UML

and the last one is the doubleCapacity


Well, first answer... as mentioned, the capacity can't be micromanaged. The best that you can do, with the ArrayList class, is to request that a capacity be available, via the ensureCapacity() method.

And second answer.... I think that you may have violated the intent of the assignment. It looks like the assignment may be expecting you to manage your own list; meaning using arrays (or some other low level data structure), to write your own list handling, instead of using the built in ones.

Henry
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok with violating :-D I agree maybe

but as someone here told me that ArrayList is the more dynamic data structure that's why I am working with it because it easier then Array

and also we usually work with ArrayList
 
Norm Radder
Ranch Foreman
Posts: 2240
28
 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:So this is a part of my class which I am practising for the coming exam


Student array created has capacity two times of Arrraylist .
                                        






[/code]
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok but do you mean that this is the correct variant for increasing the size ?

because if I am using only this line of code Eclipse shows me that the variable "Longer_Capacity" is not used ....
 
Liutauras Vilda
Marshal
Posts: 4668
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What are you trying to do with line 7? Doesn't look right.

It seems there is a requirement to create quite a few methods. One thing is apparent, that you wrote 2 methods (at least) already, and non of those compile.
Do not work on two parts at the same time. Start with one method, make sure it compiles and does what it supposed to do, and only then start on other.
 
Liutauras Vilda
Marshal
Posts: 4668
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:because if I am using only this line of code Eclipse shows me that the variable "Longer_Capacity" is not used ....

Maybe better would be to start with some simple editor and command line? That might would help you to concentrate better on the code you are writing and not to take too many hints from IDE. IDE won't solve you exercise, you need fully understand what is used and what is not and why without taking any hints from IDE.
 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:it gives me error at this line :




why are you using student=longer_capacity ? it wrong

if you using it to copy data from list to student then you already did it .

or you want to directly copy then
use public Object[] toArray() to copy arraylist to array


 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah I agree with you

I started like you are advising me but those two methods they are linking each other

as you saw after the "doubleTheListCapacity" method I have to use this one in the next method which is called "addStudent" that's why I am working with both of them

but ok the second method I've commented it now and for the previous is the same error ...
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to copy I just want to increase the size of my ArrayList using an object and after the "for" loop to increase the size
 
Liutauras Vilda
Marshal
Posts: 4668
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any other instructions apart from the diagram you showed us? If yes, please show us the relevant part.
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I apology for this but teacher for preparing for the exam he gave us only the UML diagram
 
Lalit Sahu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:yeah I agree with you

I started like you are advising me but those two methods they are linking each other

as you saw after the "doubleTheListCapacity" method I have to use this one in the next method which is called "addStudent" that's why I am working with both of them

but ok the second method I've commented it now and for the previous is the same error ...

there is no need to double the size of arraylist .it size grow dynamically according to user requirement.

For Example: If arraylist has 10 size ,now you try to add 11th element what happen ??
Show error or add it .Answer is "add 11th element without throwing any error".
how can it add element ibeyond  it  10 size? Answer is : ArrayList grow dynamically according to user need.     
 
Liutauras Vilda
Marshal
Posts: 4668
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:I don't want to copy I just want to increase the size of my ArrayList using an object and after the "for" loop to increase the size

Did you read what Henry wrote? Below..
Henry Wong wrote:Well, first answer... as mentioned, the capacity can't be micromanaged. The best that you can do, with the ArrayList class, is to request that a capacity be available, via the ensureCapacity() method.


An actual implementation needs to be probably with Array. That way sounds possible to do what you are asking and you are quite on the right path.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A completely different point: if that is for an exam you will lose many marks for the poor formatting of the code. Indent it correctly. Don't double‑space it. Don't use underscores in identifiers (except constants). Don't start variable names with CapitalLetters.
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Everyone for trying to help me ;)

Finally, I asked my teacher and he told me that this class should be a type of normal Array[] because then the method

"doubleTheListCapacity" doesn't make sense and that's why it should be Array[];

So yeah thank you once again
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constantin Cornea wrote:
Finally, I asked my teacher and he told me that this class should be a type of normal Array[] because then the method  "doubleTheListCapacity" doesn't make sense and that's why it should be Array[];


Well, it does "make sense", it just isn't guaranteed to give you exactly double the size.



Also, resizing is done automatically too, so the method wouldn't even be very useful.

Henry
 
Constantin Cornea
Ranch Hand
Posts: 37
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so much Henry, for this kind of help I was waiting
just to see some ways how to increase the size

but yeah I understood you but finally, this class should a normal array

your help will be useful for me when I will use cases like these

thanks so much
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!