返回位置索引处的元素。 (没有ArrayList)

时间:2015-02-10 19:08:30

标签: java

格式:get(index):Object。

public class MyArrayList {
    public String[] arrays = {};


    public MyArrayList() {
        arrays = new String[10];
    }

    public int get(int i){
        for(int index = 0; index< arrays.length; index++) {

        }
        return i;
    }
}

public class MyArrayListTest {

    static MyArrayList zoo = new MyArrayList();


    public static void printZoo() {
        System.out.print("The zoo now holds " + zoo.size() + " animals: ");
        for (int j = 0; j < zoo.size(); j++) System.out.print(zoo.get(j) + " ");
        System.out.println();
    }

    public static void main(String[] args) {

        System.out.println("Testing constructor, add(object) and size() ");
        zoo.add("Ant");
        zoo.add("Bison");
        zoo.add("Camel");
        zoo.add("Dog");
        zoo.add("Elephant");
        zoo.add("Frog");
        zoo.add("Giraffe");
        zoo.add("Horse");
        printZoo();
        System.out.println();
    }
}

使用此代码打印出来:

测试构造函数,添加(对象)和大小() 动物园现在拥有10只动物:0 1 2 3 4 5 6 7 8 9

显然我的get方法代码是非常错误的,但不是打印出数字,而是打印出“Ant”,“Bison”,“Camel”等等。

所有帮助都对代码表示赞赏,因为我是一名非常新的程序员。感谢。

3 个答案:

答案 0 :(得分:1)

修复获取方法

public int get(int i){
    for(int index = 0; index< arrays.length; index++) {

    }
    return i;
}

好的,让我们来看看这个吗?用户可以提供一些值..

i < 0
0 < i < size of array <-- The only valid one.
i > size of array

首先你需要检查一下!

if(i > 0 && i < arrays.length) {
    // This is a valid index!
}

好的,所以你知道它是一个有效的索引。第二步是检索值..

return arrays[i];

最后,需要设置返回类型。目前是int。在此示例中,它必须为String ..

public String get(int i)

就这么简单!当您致电printZoo()时,您会看到价值而不是他们的指数。

在您的对象上

您可以拥有类型Object的数组,而无需导入任何类。这会将类型arrays的{​​{1}}更改为..

String[]

答案 1 :(得分:0)

您的代码在技术上是正确的,但是如果您想在运行时返回字符串值,则必须将方法中返回的值更改为String,如

中所示。
public int get(int i){
for(int index = 0; index< arrays.length; index++) {

}
return i;

public String get(int i){
   return arrays[i];
}

同样在你的方法printZoo()中,你有另一个循环,所以我想象你的代码打印出重复的值。那么为什么你没有处理for循环的printZoo方法和上面显示值的get()方法

所以将你的get方法更改为我在这里的方法,一切都应该适合你

如果它不起作用,那么试试这些代码

<强> MyArrayList.java

public class MyArrayList{
public String[] arrays = {};
public int i = 0;

public MyArrayList() {
    arrays = new String[10];
}

public void add(String a)throws ListFullException{ //Add to List if Arraylist is not full
    if(i != arrays.length-1){
    arrays[i] = a;
    i++;
    }
    else{ 
        throw new ListFullException("List Full");
    }
}

public String get(int i){

    return arrays[i];
}

public int getArraySize(){
    return arrays.length;
}
}

<强> MyArrayListTest.java

public class MyArrayListTest {

static MyArrayList zoo = new MyArrayList();


public static void printZoo() {
    System.out.print("The zoo now holds " + zoo.getArraySize() + " animals: ");
    for (int j = 0; j < zoo.getArraySize(); j++) System.out.print(zoo.get(j) + " ");
    System.out.println();
}

public static void main(String[] args) {

    System.out.println("Testing constructor, add(object) and size() ");
    zoo.add("Ant");
    zoo.add("Bison");
    zoo.add("Camel");
    zoo.add("Dog");
    zoo.add("Elephant");
    zoo.add("Frog");
    zoo.add("Giraffe");
    zoo.add("Horse");
    printZoo();
    System.out.println();
}
 }

Exceptions类

<强> ListFullException.java

public class ListFullException extends RuntimeException{

public ListFullException(String m){
    super(m);
}
}

我希望这对你来说是一个很棒的学习工具,如果你觉得这对你有所帮助,那就是upvote并接受:):P

答案 2 :(得分:0)

它正在打印一个int,因为你正在调用zoo.get(j)并且get()返回ints:

public int get(int i){
for(int index = 0; index< arrays.length; index++) {

}
return i;

你需要返回一个字符串,类似于:

public String get(int i){
    return arrays[i];
}
相关问题