数组,都懂的,直接看代码吧,实现以下功能:
创建数组
查找在索引上的值
查找数组中是否含有值
删除在索引上的值
添加一个值
查找一个值在数组的位置
public class ArrayStructures {
private int[] theArray = new int[50];
private int arraySize = 10;
public void generateRandomArray(){
for (int i =0; i< arraySize;i++){
theArray[i] = (int)(Math.random()*10 + 10);
}
}
public void printArray(){
StringBuffer sb = new StringBuffer("-");
for (int i = 0; i<arraySize; i++){
sb.append("-----");
}
String septalLine= sb.toString();
System.out.println(septalLine);
for (int i = 0; i<arraySize; i++){
System.out.print("| " + i + " ");
}
System.out.println("|");
System.out.println(septalLine);
for (int i = 0; i<arraySize; i++){
System.out.print("| " + theArray[i] + " ");
}
System.out.println("|");
System.out.println(septalLine);
}
public int getValueAtIndex(int index){
if (index< arraySize)
return theArray[index];
return 0;
}
public boolean doesArrayContainThisValue(int value){
for(int i=0; i<arraySize; i++){
if(theArray[i]== value) return true;
}
return false;
}
public void deleteValueOfIndex(int index){
for(int i=index; i<arraySize; i++){
theArray[i]= theArray[i+1];
}
arraySize--;
}
public void insertValue(int value){
if(arraySize<50){
theArray[arraySize] = value;
arraySize++;
}
}
public String LinearSearchForValue(int value){
boolean valueInArray = false;
String indexsWithValue ="";
System.out.print("The value at index: ");
for(int i=0; i<arraySize; i++){
if(theArray[i] == value){
if(!valueInArray){
valueInArray = true;
}
System.out.print(i+ " ");
indexsWithValue += i+ " ";
}
}
if (!valueInArray){
indexsWithValue ="None";
System.out.println(indexsWithValue);
}
return indexsWithValue;
}
public static void main(String[] args) {
// Generate an Array
System.out.println("Create an array, and fill in random value");
ArrayStructures as = new ArrayStructures();
// Set Random value in Array
as.generateRandomArray();
// Print original array
as.printArray();
System.out.println();
System.out.println("Value at index 3");
System.out.println(as.getValueAtIndex(3));
System.out.println();
System.out.println("Does this array contain value 14");
System.out.println(as.doesArrayContainThisValue(14));
System.out.println();
System.out.println("Delete value at index 4");
as.deleteValueOfIndex(4);
as.printArray();
System.out.println();
System.out.println("Add Value 33 at the end of Array");
as.insertValue(33);
as.printArray();
System.out.println();
System.out.println("Search posittions which value is 12");
as.LinearSearchForValue(12);
System.out.println();
}
}
输出结果
Create an array, and fill in random value
---------------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---------------------------------------------------
| 19 | 12 | 13 | 12 | 18 | 12 | 11 | 16 | 18 | 10 |
---------------------------------------------------
Value at index 3
12
Does this array contain value 14
false
Delete value at index 4
----------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
----------------------------------------------
| 19 | 12 | 13 | 12 | 12 | 11 | 16 | 18 | 10 |
----------------------------------------------
Add Value 33 at the end of Array
---------------------------------------------------
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---------------------------------------------------
| 19 | 12 | 13 | 12 | 12 | 11 | 16 | 18 | 10 | 33 |
---------------------------------------------------
Search posittions which value is 12
The value at index: 1 3 4
转载于:https://blog.51cto.com/10324466/1658705