Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Get char array from String

Complete source code below will show you, how to get char array from a String. This char array we will store all characters in the String include white space.

**************************************************************************
COMPLETE SOURCE CODE FOR : GetCharArrayFromString.java
**************************************************************************


public class GetCharArrayFromString
{
public static void main(String[]args)
{
//Create a String that we want to get character from it
//You can try other String that you want
String a="Get char array from String.........";

//Create an array from type char that will store all characters from String
//Method toCharArray() will get all characters in the String
char[]storeAllStringCharacter=a.toCharArray();

//Print number of characters in char array
System.out.println("Number of characters in char array is : "+storeAllStringCharacter.length+"\n\n");

//Print all character in char array include their index in String
System.out.println("*******************************");
System.out.println("THE CHARACTER IS : ");
System.out.println("*******************************");
System.out.println();

for(int i=0; i<storeAllStringCharacter.length; i++)
{
System.out.println("Character at index "+i+" in the String is : "+storeAllStringCharacter[i]);
}
}
}


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************

Get String from char array

Complete source code below will show you, how to get String from char array.

***********************************************************************
COMPLETE SOURCE CODE FOR : GetStringFromCharArray.java
***********************************************************************


public class GetStringFromCharArray
{
public static void main(String[]args)
{
//Create an array from char type that will store all character for a String
char[]allCharacter=new char[12];

//Set all value in created array
allCharacter[0]='H';
allCharacter[1]='e';
allCharacter[2]='l';
allCharacter[3]='l';
allCharacter[4]='o';
allCharacter[5]=' ';
allCharacter[6]='W';
allCharacter[7]='o';
allCharacter[8]='r';
allCharacter[9]='l';
allCharacter[10]='d';
allCharacter[11]='!';

//Get String from created char array
String resultString=new String(allCharacter);

//Print result
System.out.println(resultString);
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

Remove element from array

*************************************************
COMPLETE SOURCE CODE FOR : RemoveElementFromAnArray.java
*************************************************


public class RemoveElementFromAnArray
{
public static void main(String[]args)
{
//Now we want to remove last element,"crocodile"
String[]animal={"lion","tiger","elephant","crocodile"};

//Step 1:Create a temp array with sizeArrayAnimal-1
String[]temp=new String[animal.length-1];

//Step 2:Copy element from "lion" to "elephant" into array temp
System.arraycopy(animal,0,temp,0,(animal.length-1));

//Make animal array point to temp array
animal=temp;

//Print all element in animal
int temp2=0;
while(temp2!=animal.length)
{
System.out.println(animal[temp2]);
temp2=temp2+1;
}
}
}


*************************************************
JUST COMPILE AND EXECUTE IT
*************************************************

Add element in array

Note : Source code below will show you how to add new element in a created array. It will add "monkey" word in an array that contains "lion","tiger","elephant","crocodile" word.

******************************************************
COMPLETE SOURCE CODE FOR : AddElementInArray.java
******************************************************


public class AddElementInArray
{
public static void main(String[]args)
{
//Create array with it's component
String[]animal={"lion","tiger","elephant","crocodile"};

//Add an element - "Monkey"
String a="monkey";

//Step 1:Create temp array (size=(arrayThatWeWantToAddElement)+1)
String[]temp=new String[(animal.length)+1];
System.arraycopy(animal,0,temp,0,animal.length);
temp[temp.length-1]=a;

//Make animal array point to temp array
animal=temp;

//Print all element in animal
int temp2=0;
while(temp2!=animal.length)
{
System.out.println(animal[temp2]);
temp2=temp2+1;
}
}
}


******************************************************
JUST COMPILE AND EXECUTE IT
******************************************************

Copy all contents from one array to other array

*****************************************
COMPLETE SOURCE CODE FOR : CopyArray.java
*****************************************


public class CopyArray
{
public static void main(String[]args)
{
//Create array with it's component
String[]animal={"lion","tiger","elephant","crocodile"};

//Create empty array with size same as animal
String[]animal2=new String[animal.length];

//Copy all element in animal into animal2
System.arraycopy(animal,0,animal2,0,animal.length);

//Print all element in animal2
int temp=0;
while(temp!=(animal.length))
{
System.out.println(animal2[temp]);
temp=temp+1;
}
}
}


*****************************************
JUST COMPILE AND EXECUTE IT
*****************************************

RELAXING NATURE VIDEO