在Java中的向量中打印出某些索引处的元素

时间:2015-10-20 23:42:14

标签: java string vector indexing

这可能是非常基本的东西,但我不太确定我应该怎样提问,因为我对此很新,所以这里就是。

我正在练习矢量以及我们可以对他们做些什么。我已成功地提示用户输入向量的元素(按照我的指示)。对于我的下一步,我必须在两个向量的每一个中打印索引i处的元素。"我得到了我应该使用的方法,但我看到的解释非常不清楚。他们在这里:

Object get (int which)

Object remove (int which)

set (int index, object element)

我如何将系统输出作为索引i的元素?

package vectorusage;

import java.util.*;

public class VectorUsage {

public static void main(String[] args) {

    Vector a = new Vector ();
    Vector b = new Vector ();

    System.out.println (a);
    System.out.println (b);

    Scanner input = new Scanner(System.in);

    String first;
    System.out.print("Please enter 4 strings.");
    first = input.next();
    a.add (first);

    String second;
    second = input.next();
    a.add (second);

    String third;
    third = input.next();
    a.add (third);

    String fourth;
    fourth = input.next();
    a.add (fourth);

    String fifth;
    System.out.print("Please enter 4 more strings.");
    fifth = input.next();
    b.add (fifth);

    String sixth;
    sixth = input.next();
    b.add (sixth);

    String seventh;
    seventh = input.next();
    b.add (seventh);

    String eighth;
    eighth = input.next();
    b.add (eighth);

    System.out.println("Vector a is size " + (a.size()) + " and contains: " + (a));
    System.out.println("Vector b is size " + (b.size()) + " and contains: " + (b));

    int i;
    System.out.println("Please enter an integer.");
    i = input.nextInt();

    System.out.println("Element at index " + i + " in Vector a is: " + ;

2 个答案:

答案 0 :(得分:0)

避免使用载体,它们会被贬低。请改用ArrayList。 使用for循环可以简化代码,如下所示,

(请注意,此代码不验证用户输入或执行错误处理)

import java.util.ArrayList;
import java.util.Scanner;

public class Test {

    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        System.out.println("Please enter 8 strings.");

        for(int i = 1; i <= 8; i++) {
            System.out.print("Please enter strings #" + i + ": ");
            numbers.add(input.nextInt());   
        }

        for(int j = 0; j < numbers.size(); j++) {

            System.out.println("Number at index " + j + " is " + numbers.get(j));
        }
    }
}

答案 1 :(得分:0)

我通常使用while和for循环的混合。 while循环用于将用户输入添加到向量中。 for循环使用索引i打印出向量的元素。我已将其设置为打印矢量的所有元素,但您可以使用if条件对其进行修改。这是我的代码,希望它有所帮助!

import java.util.*;
import java.io.*;
public class VectorUsage {

public static void main(String[]args) {
    Scanner input=new Scanner(System.in);
    Vector a=new Vector();
    int count =0;
    while(count<4)
    {
        System.out.print("Enter a string: ");
        a.addElement(input.nextLine());
        count++;
    }
    for(int i=0;i<a.size();i++)
    {
        System.out.println(a.elementAt(i));
    }
    Vector b=new Vector();
    int count1=0;
    while(count1<4)
    {
        System.out.print("Enter a string: ");
        b.addElement(input.nextLine());
        count1++;
    }
    for(int i=0;i<b.size();i++)
    {
        System.out.println(b.elementAt(i));
    }

}

}