简体   繁体   English

你如何从一个不同的class中调用一个变量,并把它添加到Java中的一个ArrayList的末尾?

[英]How do you call a variable from a different class and add it to the end of an ArrayList in Java?

I'm trying to make a program to practice ArrayLists that takes several user-inputted numbers and adds up the positive numbers entered.我正在尝试制作一个程序来练习 ArrayLists,它需要几个用户输入的数字并将输入的正数相加。 Then it should print all the positive numbers with the total at the end eg.然后它应该在末尾打印所有正数和总数。 [1, -2, 3, -4, 5] turns to [1, 3, 5, 9]. [1, -2, 3, -4, 5] 变为 [1, 3, 5, 9]。 I understand that my code is incorrect because that's NOT how you can call aspects from another class, but I've tried it several ways and I'm stuck on how to do this with ArrayLists.我知道我的代码不正确,因为这不是您可以从另一个 class 调用方面的方式,但我已经尝试了几种方法,但我一直在研究如何使用 ArrayLists 执行此操作。 Any help would be greatly appreciated!任何帮助将不胜感激!

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;
import java.io.*;


class Main {
 
  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the length of your ArrayList: ");
    int black = scan.nextInt();
    
      Scanner scan2 = new Scanner(System.in);
    ArrayList<Integer> list = new ArrayList<Integer>();
    
    System.out.println("Enter numbers: ");
    for(int i = 0; i < black; i++) {
        list.add(scan.nextInt());
    }

    /* call azure and add to the end of list */
    System.out.println(appendPosSum(newC));

  }

    
    public static int appendPosSum(ArrayList<Integer> list) {
    int azure = 0;
    ArrayList<Integer> newC = new ArrayList<Integer>();
    for(int i = 0; i < list.size(); i++) {
      if(list.get(i) > 0) {
        newC = list.get(i);
        azure += list.get(i);
      }
    }
    return azure;
    return newC;
  }
}

Recommended: Create an Instance of the Class:推荐:创建Class的实例:

You can create an instance of the other class in the Main class with the code below:您可以使用以下代码在 Main class 中创建另一个 class 的实例:

other root = new other();

Make sure the class name is correct.确保 class 名称正确。 For example, if the class is called Wonderful, the code should be:例如class叫Wonderful,代码应该是:

Wonderful root = new Wonderful();

Thus, by referencing root, you are referencing the Wonderful class.因此,通过引用 root,你引用了 Wonderful class。

Otherwise: Inherit the Class: If you want to "connect" the classes and other is similar to Main, which is not likely, you could use inheritance. Inheritance is like a family tree, or Taxonomy: a class/object shares variables or methods with another class. For example, public class Programmer extends Human. Otherwise: Inherit the Class:如果你想“连接”类和其他类似于Main,这不太可能,你可以使用inheritance。Inheritance就像一个家谱,或者分类法:一个类/对象共享变量或方法与另一个 class。例如,public class Programmer extends Human。 In this example, the Programmer inherits all the characteristics of a Human, but it has its own characteristics as well.在这个例子中,程序员继承了人类的所有特征,但也有自己的特征。 Therefore, if you wrote public class other extends Main , other would be an extension of Main with its own characteristics.因此,如果你写public class other extends Main ,other 就是 Main 的扩展,有自己的特点。 Here's an example of inheritance:这是 inheritance 的示例:

public class Panda extends Animal{
    public Panda() {
        super("Panda");
        System.out.println("Panda0");
    }
    
    public Panda(String name) {
        System.out.println("Panda1");
    }
    
    public String getName() {
        System.out.println("Panda: getName");
        return "Panda: "+ super.getName();
    }
    
    public String toString() {
        System.out.println("Panda: toString");
        return getName();
    }
}

Note that super is used to reference the method in the "parent" or extended class, so since you have two main methods, if you are using inheritance, other's main will be main() and the Main class main will be super.main() .请注意,super 用于引用“parent”或扩展 class 中的方法,因此由于您有两个 main 方法,如果您使用 inheritance,则其他的 main 将是main() ,Main class 的 main 将是super.main()

Thanks for the tips: I eventually got it working so it now looks like this:感谢您的提示:我最终让它工作了,所以它现在看起来像这样:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;
import java.io.*;

public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the length of your ArrayList: ");
    int black = scan.nextInt();

    Scanner scan2 = new Scanner(System.in);
    ArrayList<Integer> list = new ArrayList<Integer>();
    
    System.out.println("Enter numbers: ");
    for(int i = 0; i < black; i++) {
        list.add(scan.nextInt());
    }

    System.out.println(appendPosSum(list));
        
    }
    
    public static ArrayList<Integer> appendPosSum (ArrayList<Integer> list){        
        ArrayList<Integer> newList = new ArrayList<>();             
        int sum = 0;
        for(int num : list) {
            if(num > 0) {
                newList.add(num);
                sum += num;
            }
        }       
        newList.add(sum);       
        return newList;
    }
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM