Can you resize a C++ array after initialization?
C++ arrays are fixed in size.
If you need a "resizable array", you'll want to use std::vector instead of an array.
set array size on c++ after initialization
It is not possible to resize arrays like that in standard C++.
Instead, use a std::vector
. For example;
#include <vector>
class foo
{
public:
foo(std::size_t size = 0) : map(size) {}; // initialises number of elements in map
void resize(std::size_t size)
{
map.resize(size);
};
private:
std::vector<int> map;
};
The contained vector<int>
will be destroyed implicitly when an object of type foo
is destructed, and will release all resources it has allocated.
There are containers other than std::vector
which meet particular needs, but std::vector
is a good default choice in practice.
Can the size of an ArrayList be changed after it is initialized?
False. To quote from javadoc, it explains array list to be a
Resizable-array implementation of the List interface.
Further it also explains
Each ArrayList instance has a capacity. The capacity is the size of
the array used to store the elements in the list. It is always at
least as large as the list size. As elements are added to an
ArrayList, its capacity grows automatically.
Also, for an ArrayList, size can not be set while initializing. However the initial capacity can be set. Size is the number of elements in the list.
Resize an Array while keeping current elements in Java?
You can't resize an array in Java. You'd need to either:
Create a new array of the desired size, and copy the contents from the original array to the new array, using
java.lang.System.arraycopy(...);
Use the
java.util.ArrayList<T>
class, which does this for you when you need to make the array bigger. It nicely encapsulates what you describe in your question.Use
java.util.Arrays.copyOf(...)
methods which returns a bigger array, with the contents of the original array.
Assign length to array after initialization in Java?
Yes. Simply declare your array, and then in the constructor, specify the length:
public String[] elements;
public ArrayClass(int x) {
elements = new String[x];
}
change array size
No, try using a strongly typed List instead.
For example:
Instead of using
int[] myArray = new int[2];
myArray[0] = 1;
myArray[1] = 2;
You could do this:
List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
Lists use arrays to store the data so you get the speed benefit of arrays with the convenience of a LinkedList
by being able to add and remove items without worrying about having to manually change its size.
This doesn't mean an array's size (in this instance, a List
) isn't changed though - hence the emphasis on the word manually.
As soon as your array hits its predefined size, the JIT will allocate a new array on the heap that is twice the size and copy your existing array across.
How to resize array in C++?
The size of an array is static in C++. You cannot dynamically resize it. That's what std::vector
is for:
std::vector<int> v; // size of the vector starts at 0
v.push_back(10); // v now has 1 element
v.push_back(20); // v now has 2 elements
v.push_back(30); // v now has 3 elements
v.pop_back(); // removes the 30 and resizes v to 2
v.resize(v.size() - 1); // resizes v to 1
Related Topics
Get Sum of Values Stored in _M256D with Sse/Avx
Using Sfinae to Check for Global Operator<<
Overload Operators as Member Function or Non-Member (Friend) Function
How to Use a Timer in C++ to Force Input Within a Given Time
C++ Read/Write Class From/To Binary File
How to Serialize Boost::Dynamic_Bitset
Spirit-Qi: How to Write a Nonterminal Parser
Is Msiopenproduct the Correct Way to Read Properties from an Installed Product
Difference Between Initialization of Static Variables in C and C++
How to Make Cout Behave as in Binary Mode
What Is the Vtable Layout and Vtable Pointer Location in C++ Objects in Gcc 3.X and 4.X
Why Doesn't C++ Move Construct Rvalue References by Default
Sizeof(Struct) Returns Unexpected Value
C++ Template Parameter Type Inference