Found 9326 Articles for Object Oriented Programming

How to Map multi-dimensional arrays to a single array in java?

V Jyothi
Updated on 19-Feb-2020 12:15:46

2K+ Views

A two-dimensional array is nothing but an array of one dimensional arrays. Therefore to map a two dimensional array into one dimensional arrays.Create arrays equal to the length of the 2d array and, using for loop store the contents of the 2d array row by row in the arrays created above.Examplepublic class Mapping_2DTo1D {    public static void main(String args[]) {       int [][] array2D = {{7, 9, 8, 5}, {4, 5, 1, 8}, {9, 3, 2, 7}, {8, 1, 0, 9}};       int [] myArray1 = new int[array2D[0].length];       int [] myArray2 = ... Read More

How (where) are the elements of an array stored in memory?

Priya Pallavi
Updated on 30-Jul-2019 22:30:20

1K+ Views

In Java, arrays are objects, therefore just like other objects arrays are stored in heap area. An array store primitive data types or reference (to derived data) types Just like objects the variable of the array holds the reference to the array.

Can i refer an element of one array from another array in java?

Nikitha N
Updated on 16-Jun-2020 10:04:41

389 Views

Yes, you can −int [] myArray1 = {23, 45, 78, 90, 10}; int [] myArray2 = {23, 45, myArray1[2], 90, 10};But, once you do so the second array stores the reference of the value, not the reference of the whole array. For this reason, any updating in the array will not affect the referred value −ExampleLive Demoimport java.util.Arrays; public class RefferencingAnotherArray {    public static void main(String args[]) {       int [] myArray1 = {23, 45, 78, 90, 10};       int [] myArray2 = {23, 45, myArray1[2], 90, 10};       System.out.println("Contents of the ... Read More

What are variable length (Dynamic) Arrays in Java?

Srinivas Gorla
Updated on 19-Feb-2020 12:12:03

4K+ Views

In Java, Arrays are of fixed size. The size of the array will be decided at the time of creation. But if you still want to create Arrays of variable length you can do that using collections like array list.Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class AddingItemsDynamically {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array :: ");       int size = sc.nextInt();       String myArray[] = new String[size];       System.out.println("Enter elements of the array (Strings) :: ");       for(int i=0; i

How to create a generic array in java?

Abhinanda Shri
Updated on 30-Jul-2019 22:30:20

169 Views

No, we can’t create generic arrays in java.

How to add items to an array in java dynamically?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:20

9K+ Views

Since the size of an array is fixed you cannot add elements to it dynamically. But, if you still want to do it then, Convert the array to ArrayList object.Add the required element to the array list.Convert the Array list to array.Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class AddingItemsDynamically {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array :: ");       int size = sc.nextInt();       String myArray[] = new String[size];       System.out.println("Enter elements of the array ... Read More

How to overwrite a specific chunk in a byte array using java?

Abhinaya
Updated on 16-Jun-2020 10:02:01

1K+ Views

Java provides a ByteBuffer class which allows you to wrap an array into a byte buffer using its wrap() method. Once you did that you can replace the contents of the buffer using the position(): To select the starting position and, put(): To replace the data methods:ExampleLive Demoimport java.nio.ByteBuffer; public class OverwriteChunkOfByteArray {    public static void main(String args[]) {       String str = "Hello how are you what are you doing";       byte[] byteArray = str.getBytes();       System.out.println("Contents of the byet array :: ");             for(int i = 0; i

What are the different types of keywords in Java?

varma
Updated on 18-Feb-2020 11:33:18

2K+ Views

Following are the different types of keywords in java—Access modifiers − private, protected, public.Class, method, variable modifiers− abstract, class, extends, final, implements, interface, native, new, static, strictfp, synchronized, transient, volatile.Flow control− break, case, continue, default, do, else, for, if, instanceof, return, switch, while.Package control− import, package.Primitive types− boolean, byte, char, double, float, int, long, short.Error handling− assert, catch, finally, throw, throws, try.Enumeration− enum.Others− super, this, void.Unused− const, goto.

How to declare Java array with array size dynamically?

Ramu Prasad
Updated on 19-Feb-2020 12:09:26

2K+ Views

To declare array size dynamically read the required integer value from the user using Scanner class and create an array using the given value:Exampleimport java.util.Arrays; import java.util.Scanner; public class PopulatingAnArray {    public static void main(String args[]) {       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       System.out.println("Enter the elements of the array one by one ");             for(int i = 0; i

Is null a keyword in Java?

usharani
Updated on 30-Jul-2019 22:30:20

438 Views

No, null is not a keyword. Though they seem like keywords null, true and, false are considered as literals in Java.

Advertisements