Adding deletes and copies of arrays

Source: Internet
Author: User

The addition of arrays
public class Arradddemo {
static void AddLast () {
Add a number 100 to the end of the array
Int[] Oldarr=new int[]{45,56,12,78,88};
Int[] Newarr=new int[oldarr.length+1];
for (int i=0;i<oldarr.length;i++) {
Newarr[i]=oldarr[i];
}
newarr[newarr.length-1]=100;

for (int i=0;i<newarr.length;i++) {
System.out.println (Newarr[i]);
}
}


Insert to first position of array
static void AddFirst () {
Int[] Oldarr=new int[]{45,56,12,78,88};
Int[] Newarr=new int[oldarr.length+1];
for (int i=0;i<oldarr.length;i++) {
Newarr[i+1]=oldarr[i];
}

newarr[0]=100;

for (int i=0;i<newarr.length;i++) {
System.out.println (Newarr[i]);
}
}

Put it in a third position.
static void Putthirdpos () {
Int[] Oldarr=new int[]{45,56,12,78,88};
Int[] Newarr=new int[oldarr.length+1];
int index=2;
for (int i=0;i<oldarr.length;i++) {
if (I<index) {
Newarr[i]=oldarr[i];
}else{
Newarr[i+1]=oldarr[i];
}
}
newarr[index]=100;

for (int i=0;i<newarr.length;i++) {
System.out.println (Newarr[i]);
}
}


public static void Main (string[] args) {
Arradddemo.addlast ();
AddFirst ();
Putthirdpos ();
}
}

Import Java.util.Scanner;

//delete elements in an array
public class Arraydeletedemo {
/*
* 1. Determine which element to delete
* 2. Determine the subscript for the deleted element
* 3. Copy the original element to the new array
*/< Br>public static void Main (string[] args) {
int[] arr=new int[]{1,2,3,4,5,6,7};
Int[] Newarr=new int[arr.length-1];
Scanner input=new Scanner (system.in);
System.out.println ("Please enter the element to be deleted:");
int num=input.nextint ();
int index=-1;

//Find the subscript for the deleted element
for (int i=0;i<arr.length;i++) {
if (Num==arr[i]) {
index=i;
Break
}
}

//Copy the original element to the new array
for (int i=0;i<arr.length;i++) {
if (i<index) {
Newarr[i]=arr[i];
} else{
Newarr[i]=arr[i+1];
}
}

//Print new array
for (int i=0;i<newarr.length;i++) {
System.out.println ("" +newarr[i]);
}

}
}

//copy of array
public class Arraycopydemo {
public static void Main (string[] args) {
int[] src=new int[]{ 1,2,3,4,5,6,7,8,9,10}; Source Array
int[] dest=new int[src.length];//0 0 0 0 0 3 4 5 6 0
//demand is the show off in Arr 3,4,5,6 copied to the new array from subscript 5 to
Print (dest);
System.out.println ();

System.arraycopy (SRC, 2, dest, 5, 5);
Arraycopydemo.copy (src,2,dest,5,4);
Print (dest);
/*
* SRC indicates that the original array
* 2 means the subscript position of the first number is copied
* Dest means the new array
* 5 indicates where to start copying from the newly array
* 4 means the number of copied elements
*
* j Ava.lang system This class
* public static native void Arraycopy (Object src, int srcpos,object dest, int destpos,int length);
* Local, C + + implementations written, Java direct call
*
*/
}

/*static void Copy (int[] arr,int arrpos,int[] Newarr,int NEWARRP Os,int length) {
for (int i=arrpos;i<arrpos+length;i++) {
Newarr[newarrpos]=arr[i];
newarrpos++;
}
}*/


static void print (int[] arr) {
for (int i=0;i<arr.length;i++) {
System.out.print (arr [i]+ "");
}
}
}

Adding deletes and copies of arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.