answersLogoWhite

0

If the two arrays are a simple division of the larger array (such as splitting it in half), you can use pointers to mark the start address of each sub-array:

int a[10];

int* p1 = &a[0]; // point to first half

int* p2 = &a[5]; // point to second half

You should also use unsigned integer variables to keep track of the size of each sub-array:

unsigned p1size = 5, p2size=5;

It's usually best to use a structure to keep array pointers and size variables together in one place:

typedef struct intarray {

int* ptr;

unsigned size;

}

If you need to split the array in a non-contiguous manner you will need to create new arrays that are at least as large as the original array, and then copy the appropriate elements to the appropriate sub-array. Once copied, you can (optionally) shrink the sub-arrays to eliminate any unused elements.

User Avatar

Wiki User

9y ago

What else can I help you with?

Continue Learning about Engineering

What is dimentions arrays in java?

You can make arrays with any number of dimensions (depending on RAM limitations, of course). However, internally, a two-dimensional array (for example) is stored as an array of arrays; that is, each first-level array contains an array of the second level. Similarly with higher dimensions.


What does a 2 dimensional array do?

A two dimensional array is a one-dimensional array of one-dimensional arrays. That is, just as we can have an array of integers, we can also have an array of integer arrays. This idea can be extended such that we can have an array of two-dimensional arrays (a three-dimensional array), and so on. We typically use a two-dimensional array to represent a table of rows and columns, where each row is a one-dimensional array.


Are arrays objects?

Yes. Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives


How can loops be used to process arrays?

Loops can be used to iterate or walk through an array. For example, suppose you have an array already initialized (we'll call it "array"): int target = -1; for (i=0; i < array.length(); i++){ if (array[i] = target){ //do something } } Here, we will walk through an array (note that arrays are zero indexed) using a loop to make sure we hit each element of the array. In our loop, we start at the head (or first element) and iterate over each element.


Dynamically allocated array waste storage and time?

It's actually not true. In order to make a good program which can work with big arrays you have to use dynamic arrays because you can cleam memory used by dymanic arrays any time. For static arrays is not true, memery which was reserved for static arrays will be available for other applications only when you finish working with your application (which is working with static arrays).

Related Questions

What is dimentions arrays in java?

You can make arrays with any number of dimensions (depending on RAM limitations, of course). However, internally, a two-dimensional array (for example) is stored as an array of arrays; that is, each first-level array contains an array of the second level. Similarly with higher dimensions.


Is an array is a collection of characters that can be fixed or variable?

No. An array is a collection of objects of any type, such as doubles, not just characters. You can even have arrays of arrays, or arrays of structs. In C, the size of an array is fixed, but it is possible to write code that will allow you to manually make it variable in size.


What does a 2 dimensional array do?

A two dimensional array is a one-dimensional array of one-dimensional arrays. That is, just as we can have an array of integers, we can also have an array of integer arrays. This idea can be extended such that we can have an array of two-dimensional arrays (a three-dimensional array), and so on. We typically use a two-dimensional array to represent a table of rows and columns, where each row is a one-dimensional array.


Are arrays objects?

Yes. Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives


How can loops be used to process arrays?

Loops can be used to iterate or walk through an array. For example, suppose you have an array already initialized (we'll call it "array"): int target = -1; for (i=0; i < array.length(); i++){ if (array[i] = target){ //do something } } Here, we will walk through an array (note that arrays are zero indexed) using a loop to make sure we hit each element of the array. In our loop, we start at the head (or first element) and iterate over each element.


How do you accept an array in java?

Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives. Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier. Declaring an array of primitives: int[] Values; // brackets before name (recommended) int Values []; // brackets after name (legal but less readable)


Dynamically allocated array waste storage and time?

It's actually not true. In order to make a good program which can work with big arrays you have to use dynamic arrays because you can cleam memory used by dymanic arrays any time. For static arrays is not true, memery which was reserved for static arrays will be available for other applications only when you finish working with your application (which is working with static arrays).


Which is faster array or arraylist?

Array Lists and Arrays can be compared to one another. Actually an Array List is nothing but a growable array that provides us with a lot more features than traditional Arrays. Apart from this - they are both same - used to hold a group of java objects.


What is an array and explain?

Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives.Arrays are efficient, but most of the time you'll want to use one of the Collection types from java.util (including HashMap, ArrayList, TreeSet). Collection classes offer more flexible ways to access an object (for insertion, deletion, and so on) and unlike arrays, can expand or contract dynamically as you add or remove elements (they're really managed arrays, since they use arrays behind the scenes).Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.Declaring an array of primitives:int[] Values; // brackets before name (recommended)int Values []; // brackets after name (legal but less readable)// spaces between the name and [] legal, but badDeclaring an array of object references:Ferrari[] Ferraris; // RecommendedFerrari Ferraris[]; // Legal but less readableWhen declaring an array reference, you should always put the array brackets immediately after the declared type, rather than after the identifier (variable name). That way, anyone reading the code can easily tell that, for example, Values is a reference to an int array object, and not an int primitive.We can also declare multidimensional arrays, which are in fact arrays of arrays. This can be done in the following manner:String[][][] occupantName; // recommendedString[] ManagerName []; // Real bad coding practice, but legalThe first example is a three-dimensional array (an array of arrays of arrays) and the second is a two-dimensional array. Notice in the second example we have one square bracket before the variable name and one after. This is perfectly legal to the compiler, but if you write code like this, anyone who is going to manage your code in future is sure going to curse you badly.It is never legal to include the size of the array in your declaration. Yes, before you ask me, let me admit that you can do that in some other languages, which is exactly why you might see a question or two that include code similar to the following:int[5] races;The preceding code won't make it past the Java compiler. Remember, the JVM doesn't allocate space until you actually instantiate the array object. That's when size matters and not when you declare the array variable.


How many arrays can you make with the number 16?

The number of arrays you can make with the number 16 depends on how you define "arrays." If you're referring to the factors of 16, they are 1, 2, 4, 8, and 16, which can form rectangular arrays of various dimensions (e.g., 1x16, 2x8, 4x4). In terms of combinations or arrangements of the number 16 in an array (like in permutations), the possibilities would be significantly greater, depending on the context and constraints you apply.


What do you mean by array in java explain in details?

Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives.Declaring an ArrayArrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.Declaring an array of primitives:int[] Values; // brackets before name (recommended)int Values []; // brackets after name (legal but less readable)// spaces between the name and [] legal, but badDeclaring an array of object references:Ferrari[] Ferraris; // RecommendedFerrari Ferraris[]; // Legal but less readableWhen declaring an array reference, you should always put the array brackets immediately after the declared type, rather than after the identifier (variable name). That way, anyone reading the code can easily tell that, for example, Values is a reference to an int array object, and not an int primitive.We can also declare multidimensional arrays, which are in fact arrays of arrays. This can be done in the following manner:String[][][] occupantName; // recommendedString[] ManagerName []; // Real bad coding practice, but legalThe first example is a three-dimensional array (an array of arrays of arrays) and the second is a two-dimensional array. Notice in the second example we have one square bracket before the variable name and one after. This is perfectly legal to the compiler, but if you write code like this, anyone who is going to manage your code in future is sure going to curse you badly.It is never legal to include the size of the array in your declaration. Yes, before you ask me, let me admit that you can do that in some other languages, which is exactly why you might see a question or two that include code similar to the following:int[5] races;The preceding code won't make it past the Java compiler. Remember, the JVM doesn't allocate space until you actually instantiate the array object. That's when size matters and not when you declare the array variable.


What is the difference between a pointer and an array in C programming?

An array is a contiguous block of memory containing one or more elements of the same type. The array identifier is a reference to that block of memory. References do not require any memory other than the memory they reference. References are not variables -- they are merely aliases for memory addresses. A pointer is a variable that can store any reference and provides indirect access to that reference. Pointers can be used to allocate arrays dynamically, which means memory is required not only for the array, but also for the pointer to store the starting memory address of the array. Once allocated, the array can be referenced directly, but the pointer is still required in order to release the memory allocation when it is no longer required. Arrays employ pointer arithmetic to locate individual elements by their zero-based index, which is essentially an offset from the reference multiplied by the size of the element type. However arrays and pointers are not the same from a programming standpoint. Your compiler may well implement references as pointers, but that is only of concern to compiler authors, not programmers. Two-dimensional dynamic arrays make use of a pointer-to-pointer variable to point to a one-dimensional array of pointers, each of which points to a one-dimensional array of the actual elements. A three-dimensional array employs a pointer-to-pointer-to-pointer to point to a one-dimensional array of pointer-to-pointer variables, each of which points to a two-dimensional array. And so on. Static arrays use less memory as there is no need to maintain arrays of pointers, but static arrays are only useful when the number of elements and dimensions are known at compile time. At runtime, arrays of pointers are required over and above the array elements themselves, in order to both allocate and deallocate the memory, as well as obtain references to the elements in the array. Pointers also have uses beyond that of dynamic arrays, including allocating any type of memory of any size, and pointing at functions which can then be passed as arguments to other functions.