【问题标题】:Printing out elements at certain indexes in vectors in Java在Java中打印出向量中某些索引处的元素
【发布时间】:2015-10-20 23:42:14
【问题描述】:

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

我正在练习矢量以及我们可以对它们做什么。我已成功提示用户输入向量的元素(根据我的指示)。对于我的下一步,我必须“打印出两个向量中索引 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: " + ;

【问题讨论】:

  • 你不应该使用vectors。它们被视为已弃用。检查here

标签: java string vector indexing


【解决方案1】:

避免使用向量,它们被贬低了。请改用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));
        }
    }
}

【讨论】:

    【解决方案2】:

    我通常混合使用 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));
        }
    
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      • 2016-01-28
      相关资源
      最近更新 更多