java中数组作为类的成员变量怎么用
时间: 2024-05-02 19:20:03 浏览: 17
在Java中,可以将数组作为类的成员变量声明,并根据需要初始化它。以下是一个示例:
```
public class MyClass {
private int[] myArray;
public MyClass(int size){
myArray = new int[size];
}
public void setArrayValue(int index, int value){
myArray[index] = value;
}
public int getArrayValue(int index){
return myArray[index];
}
}
```
在上面的示例代码中,我们首先声明了一个名为myArray的私有整数数组,并在构造函数中对其进行了初始化。然后,我们提供了一些简单的setter和getter方法来设置和获取数组中的值。使用时,只需创建一个MyClass对象,然后使用setter方法设置数组中的值,并使用getter方法获取数组中的值即可。
相关问题
java string数组为成员变量
在 Java 中,可以将数组作为类的成员变量来声明。例如,如果你想在一个类中声明一个 `String` 数组作为成员变量,可以使用以下代码:
```java
public class MyClass {
public String[] myArray;
// 构造函数
public MyClass() {
myArray = new String[3];
myArray[0] = "hello";
myArray[1] = "world";
myArray[2] = "java";
}
// 其他方法
// ...
}
```
在这个例子中,`myArray` 是 `MyClass` 类的一个成员变量,它被声明为 `public`,因此可以在类的内部和外部直接访问。在这个类的构造函数中,我们初始化了 `myArray` 数组,然后就可以在类的其他方法中使用 `myArray` 数组了。
java数组类成员多次排序
可以使用Java中的Arrays类来对数组进行排序。如果需要多次对同一个数组进行排序,可以将其封装成一个类的成员变量,然后在类中定义多个排序方法,每个方法都调用Arrays.sort()方法进行排序。
例如,下面是一个示例类,包含了三个不同的排序方法:
```
import java.util.Arrays;
public class SortedArray {
private int[] array;
public SortedArray(int[] array) {
this.array = array;
}
public void sortAscending() {
Arrays.sort(array);
}
public void sortDescending() {
Arrays.sort(array);
reverse();
}
public void sortEvenOdd() {
int[] even = new int[array.length];
int[] odd = new int[array.length];
int evenIndex = 0, oddIndex = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 == 0) {
even[evenIndex++] = array[i];
} else {
odd[oddIndex++] = array[i];
}
}
Arrays.sort(even, 0, evenIndex);
Arrays.sort(odd, 0, oddIndex);
System.arraycopy(even, 0, array, 0, evenIndex);
System.arraycopy(odd, 0, array, evenIndex, oddIndex);
}
private void reverse() {
for (int i = 0; i < array.length / 2; i++) {
int temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
}
public void print() {
System.out.println(Arrays.toString(array));
}
}
```
在上述示例中,sortAscending()方法调用Arrays.sort()方法对数组进行升序排序;sortDescending()方法先使用Arrays.sort()方法进行排序,然后再将数组反转,实现降序排序;sortEvenOdd()方法将数组中的偶数和奇数分别排序,然后再将两个部分合并,实现奇偶排序。
可以根据需要来定义更多的排序方法。使用这个类,可以方便地对同一个数组进行多次排序:
```
int[] array = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
SortedArray sortedArray = new SortedArray(array);
sortedArray.sortAscending();
sortedArray.print(); // [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
sortedArray.sortDescending();
sortedArray.print(); // [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
sortedArray.sortEvenOdd();
sortedArray.print(); // [2, 4, 6, 1, 1, 3, 3, 5, 5, 5, 9]
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)