Array self-experience

Source: Internet
Author: User
Tags array length

Please enter the names of the 5 students in the class (meaning that you enter the names of the five students into the array
where the Declaration and loop of the array is used, and the function that goes back to the String)
String [] names = new String[5];//declares a function of length 5
int i=0;//declares A variable i
For (i=0;i<nums.length;i++) {
System.out.println ("please enter" + (i+1) + "student's Name")
Nums[i]=input.next//assigns a customer input string to a value corresponding to the ordinal
}
For (i=0;i<nums.length;i++) {
System.out.pring (nums[i]+ ' \ t ');//create a loop that is less than the length of the array, enter the contents of each turn and wrap


}

System.out.println ();
//array Lookup
//find The array if there is a name you entered, if there is no output does not show the
This problem depends on the array of things to compare, and some words will not show the output is Not. Then the comparison
will use the for loop, if there is no need to add if Judgment. Then at least there must be two ways.
System.out.pringln ("enter The name of the student you want to enter");
String findName =input.next ();//get The value that the customer is looking for;
Boolean isfind =false ; Set a true or False label
for (int I=0;i<nums.length;i++) {//loop to build an array
if (findname.equals (names[i])) {// This is a comparison between the string entered by the customer and the value of item I
Isfind =1;
break;
}}
If (isfind>0) {
System.out.println ("found");

}else
System.out.println ("not found");

}
Titled Enter a number in the array to find, if found, the output of this number in the array subscript is not found the display is not found
According to test instructions, we know that finding, need a loop, whether to find the need for a judgment, then this problem must have at least 1
There is also a loop to Judge.
int [] nums={25,54,34,345,5,3};
int FindIndex =-1;//here gives-1 is for good distinction

System.out.println ("please Enter all the numbers you Find")
int FID =input.nextint ();//create A variable FID to accept the number entered by the customer
For (int I=0;i<nums.length;i++) {//create A For loop to find the required number
If (find ==nums[i]) {//perform an If judgment if the value of item I is equal to the value entered by the customer
Findindex=i;//assigns the number I to the variable FindIndex records it
Break;//this cycle is to find out whether the number of customer input is found, since found there is no circular meaning;
}
If (findindex>0) {//if found, record it as Found.
System.out.println ("meaning to Find")

}else

System.out.println ("not Found")
}

After finding the data to delete the location, then the array length-1 to form a new array;
Note that according to test instructions, we are required to find the number under the array instead of it and then form a new array, we need at least two loops
For storing, declaring two new arrays is to delete the array data and form a new array
int [] nums ={15,54,35,32};//we first defined an array of last Names.
int FindIndex =-1;//the Same thing. 1 is the same as the above to record the distinction

System.out.println ("please Enter the array you want to find:");//prompt user input
int finf = Input.nextint ();//get the User's input

For (int I=0;i<nums.length;i++) {//create a loop first to find out where the number is located
If (nums[i]==find) {//comparison is equal
Findindex=i;//to Record values
Break;//end Loop
}
}
If (findindex>0)//if the customer input value is yes
System.out.println ("found, in the array subscript of the" +findindex+ "item")

We've got a location, we're going to start creating arrays.
Nums[findidex]=nums[nums.length-1];//because the request is to delete the selected position array and the new array
Minus 1
int [] newnums =new int[nums.length-1];//creates A new array newnums, and assigns its length to a value of
Nums.length-1
For (int I=0;i<newnums.length;i++) {//create a loop that iterates through the new array
Nuwnums[i]=nums[i];//all values in the old array are assigned to the new array
}
For (int I=0;i<newnums.length;i++) {

System.out.println (newnums[i]);
}else{

System.out.println ("not found");
}
delete, and keep the order
int [] Nums ={23,35,31,3};
int FindIndex =-1;
System.out.println ("please Enter the number you want to find");
int find =inpur.nextint ();
For (int I=0;i<nums.length;i++) {
If (find==nums[i]) {
FindIndex =i;
Break
}
}


If (findindex>=0) {
System.out.printlb ("")
int [] newnums =new int[nums.length-1];//new array minus one for old array

For (int I=findindex;i<nums.length-1;i++)
Nums[i]=nums[i+1];//starting with the value to be changed forward with the back of the old array one pushes this number Out.
}
For (int I=0;i<newnums.length;i++) {
Newnums[i]=nums[i];//all values in the old array are assigned to the new array.
}

For (int i=0; i<newnums.length; i++
System.out.println (newnums[i]);//traverse Output

string[] names =new string[5]; set an array of length 5
names[0]= "sad";
names[1]= "sads";
names[2]= "ase";
For (int I=0;i<names.length;i++) {
If (names[i]==null) {
names[i]= "jackson";
Break
}
}
For (int I=0;i<names.length;i++) {
Sysrem.out.rintln (newnums[i]);

Array self-experience

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.