How to resize an array in java

 
In General terms, As we know that we cannot resize an array once we initialized because array defination says that an array is an fixed size,sequence and collection of similar data type. But It is possible to resize an array, resizing an array is nothing but copy the content of old array to new index array as per our requirement. Following code snippets is usefull for resize an array. Java.lang.System.arraycopy() Method is responsible for resizing an array.
File Name : ResizeArray.java
 
What is Java.lang.System.arraycopy() Method :
The java.lang.System.arraycopy() method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest.The number of components copied is equal to the length argument.
The components at positions srcPos through srcPos + length - 1 in the source array are copied into positions destPos through destPos + length - 1, respectively, of the destination array.
Syntax :
 
Parameters:
  • src       -- This is the source array.
  • srcPos -- This is the starting position in the source array.
  • dest -- This is the destination array.
  • destPos -- This is the starting position in the destination data.
  • length -- This is the number of array elements to be copied.
Return Value: Method does not return any value.
Exception Details:
  • IndexOutOfBoundsException  --if copying would cause access of data outside array bounds.
  • ArrayStoreException -- if an element in the src array could not be stored into the dest array because of a type mismatch.
  • NullPointerException -- if either src or dest is null.
Another Example : File Name :TestSystemDemo.java
output : array2 = 0 10 20 30 40

posted under | 0 Comments

How to read XML file in java

How To Read/Parse XML File In Java

Here you will find the program which read/parse xml file. It will provide you a basic guide to parse your XML document, Your codding may be different as per your requirement. Here you will find mathod called getAllStudentNames(String fileName). Please provide fileName as full path of your XML file source.
File Name : XMLParser.java
 
File Name : student.xml
 

posted under | 1 Comments

How to take screen shots in Java

How to take screen shots in Java

Following piece of code in Java that takes the screen shot of your desktop and save it in a PNG file. This example uses java.awt.Robot class to capture the screen pixels and returns a BufferedImage. Java.awt.Robot class is used to take the control of mouse and keyboard. Once you get the control, you can do any type of operation related to mouse and keyboard through your java code. This class is used generally for test automation. Copy and paste following code in your Java class and invoke the method captureScreen() with file name as argument. The screen shot will be stored in the file that you specified in argument. Please enter the full file path.
 

posted under | 0 Comments

Manipulate ISO Dates in Javascript

Following code helps you to find total number of weeks in javascript as well as getISOYear, getISOWeek.

Manipulate ISO Dates in Javascript

posted under , | 0 Comments

Date Manipulation In Java

 
This Class will become very usefull when you are dealing with Dates in Java. This class provides method which you can call based on your needs.

File Name : DateManipulation.java

 

posted under , | 0 Comments
Newer Posts Older Posts Home

Followers

Powered by Blogger.

Populares

Custom Search