简体   繁体   中英

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. 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]. 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. 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:

You can create an instance of the other class in the Main class with the code below:

other root = new other();

Make sure the class name is correct. For example, if the class is called Wonderful, the code should be:

Wonderful root = new Wonderful();

Thus, by referencing root, you are referencing the 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. 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. Here's an example of 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() .

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;
    }
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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