Java是一种面向对象编程语言,一般用于开发网络应用、运算、大型游戏以及移动设备等方面。在Java的编程中,代码示例是很重要的参考工具。本文将分享一些Java示例代码,包括基础语法、数据类型、控制流、常见数据结构和算法等内容。
一、基础语法
1.输出Hello World
```
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
```
2.读取键盘输入
```
import java.util.Scanner;
public class KeyboardInput {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scan.nextInt();
System.out.println("The number entered is: " + num);
}
}
```
3.计算整数之和
```
public class SumIntegers {
public static void main(String[] args) {
int num1 = 5;
int num2 = 10;
int sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
}
}
```
4.使用for循环遍历数组
```
public class ArrayLoop {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
```
二、数据类型
1.整数类型
```
public class IntegerDataType {
public static void main(String[] args) {
byte num1 = 127;
short num2 = 32767;
int num3 = 2147483647;
long num4 = 9223372036854775807L;
System.out.println("Byte: " + num1);
System.out.println("Short: " + num2);
System.out.println("Integer: " + num3);
System.out.println("Long: " + num4);
}
}
```
2.浮点类型
```
public class FloatingPointDataType {
public static void main(String[] args) {
float num1 = 3.14f;
double num2 = 3.1415926535;
System.out.println("Float: " + num1);
System.out.println("Double: " + num2);
}
}
```
3.布尔类型
```
public class BooleanDataType {
public static void main(String[] args) {
boolean flag = true;
System.out.println(flag);
}
}
```
4.字符类型
```
public class CharDataType {
public static void main(String[] args) {
char ch = 'A';
System.out.println(ch);
}
}
```
三、控制流
1.if-else条件语句
```
public class IfElseStatement {
public static void main(String[] args) {
int num = 10;
if(num%2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}
```
2.switch语句
```
public class SwitchStatement {
public static void main(String[] args) {
int num = 2;
switch(num) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
default:
System.out.println("Invalid number");
}
}
}
```
3.while循环
```
public class WhileLoop {
public static void main(String[] args) {
int num = 5;
while(num < 10) {
System.out.println(num);
num++;
}
}
}
```
4.do-while循环
```
public class DoWhileLoop {
public static void main(String[] args) {
int num = 5;
do {
System.out.println(num);
num++;
} while(num < 10);
}
}
```
四、常见数据结构
1.数组
```
public class ArrayExample {
public static void main(String[] args) {
int[] arr = {10,20,30,40,50};
for (int i = 0; i < arr.length; i++) {
System.out.println("Element at index " + i + " : "+ arr[i]);
}
}
}
```
2.链表
```
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class LinkedListExample {
Node head;
public void add(int data) {
Node nodeToAdd = new Node(data);
if(head == null) {
head = nodeToAdd;
return;
}
Node node = head;
while(node.next != null) {
node = node.next;
}
node.next = nodeToAdd;
}
public void print() {
Node node = head;
while(node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public static void main(String[] args) {
LinkedListExample list = new LinkedListExample();
list.add(1);
list.add(2);
list.add(3);
list.print();
}
}
```
3.栈
```
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack stack.push("Java"); stack.push("Python"); stack.push("C++"); System.out.println(stack.pop()); System.out.println(stack.peek()); } } ``` 4.队列 ``` import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queue queue.offer("Java"); queue.offer("Python"); queue.offer("C++"); System.out.println(queue.poll()); System.out.println(queue.peek()); } } ``` 五、常见算法 1.冒泡排序 ``` public class BubbleSort { public static void main(String[] args) { int[] arr = {5,2,8,3,1,7}; int temp; for(int i = 0; i < arr.length-1; i++) { for(int j = 0; j < arr.length-1-i; j++) { if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for(int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } } ``` 2.快速排序 ``` public class QuickSort { public static void main(String[] args) { int[] arr = {5,2,8,3,1,7}; int low = 0; int high = arr.length - 1; quickSort(arr, low, high); System.out.println(Arrays.toString(arr)); } public static void quickSort(int[] arr, int low, int high) { if(low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; for(int j = low; j < high; j++) { if(arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } } ``` 3.归并排序 ``` public class MergeSort { public static void main(String[] args) { int[] arr = {5,2,8,3,1,7}; int low = 0; int high = arr.length - 1; mergeSort(arr, low, high); System.out.println(Arrays.toString(arr)); } public static void mergeSort(int[] arr, int low, int high) { if(low < high) { int middle = (low + high) / 2; mergeSort(arr, low, middle); mergeSort(arr, middle + 1, high); merge(arr, low, middle, high); } } public static void merge(int[] arr, int low, int middle, int high) { int[] temp = new int[high - low + 1]; int i = low; int j = middle + 1; int k = 0; while(i <= middle && j <= high) { if(arr[i] < arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; } } while(i <= middle) { temp[k++] = arr[i++]; } while(j <= high) { temp[k++] = arr[j++]; } for(int x = 0; x < temp.length; x++) { arr[low + x] = temp[x]; } } } ``` 总结: 本文介绍了Java示例代码,包括基础语法、数据类型、控制流、常见数据结构和算法等内容。本文中的示例代码只是一些基础示例,Java编程是一个广阔的领域,还有很多需要学习和探索的内容。要成为一名优秀的Java程序员,需要不断学习和实践,掌握基础知识之后,还需要深入研究Java高级编程,如Java多线程、Java虚拟机、网络编程等方面,这些都是Java编程中比较重要的部分,需要加强学习和掌握。在编程中,还需要注意代码的可读性和可维护性,保持良好的编码习惯和规范。 壹涵网络我们是一家专注于网站建设、企业营销、网站关键词排名、AI内容生成、新媒体营销和短视频营销等业务的公司。我们拥有一支优秀的团队,专门致力于为客户提供优质的服务。 我们致力于为客户提供一站式的互联网营销服务,帮助客户在激烈的市场竞争中获得更大的优势和发展机会!
发表评论 取消回复