首页 / 算法 / 数据结构和算法-001 数组
数据结构和算法-001 数组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了数据结构和算法-001 数组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3606字,纯文字阅读大概需要6分钟。
内容图文
数组,都懂的,直接看代码吧,实现以下功能:
创建数组
查找在索引上的值
查找数组中是否含有值
删除在索引上的值
添加一个值
查找一个值在数组的位置
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
本文出自 “10314466” 博客,请务必保留此出处http://10324466.blog.51cto.com/10314466/1658705
原文:http://10324466.blog.51cto.com/10314466/1658705
内容总结
以上是互联网集市为您收集整理的数据结构和算法-001 数组全部内容,希望文章能够帮你解决数据结构和算法-001 数组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。