public ArrayList<Integer> onlyPrimes(int[] numbers) {
ArrayList<Integer> primes = new ArrayList<Integer>();
…
}
my mind can’t wrap around what is happening with these 2 lines
I can understand your confusion. There is a lot going on in those two lines.
Line # 1
The first line defines the signature of a method. The name of the method will be onlyPrimes. This method will return a reference to an object, an instance of the class ArrayList (or return a null). The ArrayList object will contain zero, one, or more elements, each element being a reference to an object of the class Integer. When a programmer calls this onlyPrimes method, she will pass an array ([]) of primitive int values (or pass a null). Within the body of our onlyPrimes method, this passed array will be known as numbers.
By this declaration, any calling programmer knows that if they hand over an array of primitive int values, they get back a collection of Integer objects.
You may be getting especially confused by the fact that Java has two parallel type systems: primitives and objects:
- Java was intended to be a thoroughly object-oriented language.
- The primitives type system was also put into Java to (a) make learning easier to the majority of programmers then who were used to C-like languages, and (b) facilitate porting C code into Java.
The two type systems (objects & primitives) are bridged by auto-boxing with wrapper classes such as Integer for int.
Line # 2
The next line is the first line in the body of our method named onlyPrimes. Notice how I corrected your indenting to make clear this relationship between (a) the declaration of a method, and (b) the body of that method. The { curly-brace marks the opening of the body of the declared method.
This first line of the method body declares an object reference variable named primes. That variable shall hold a reference to an instance of the class ArrayList. This instance is a collection of objects of the class Integer. The EQUALS sign assigns an object reference to our variable primes.
The code after the EQUALS sign instantiates a new collection, an object of the class ArrayList. That collection is empty, holding no elements, but is capable of holding references to objects of the class Integer. The instantiation returns a reference to the new collection object. That reference is assigned as the content of the object reference variable we declared with name of primes.
Tutorials
I suggest you study a simpler tutorial that goes over the various parts of this code.
- A good place to start is The Java Tutorials, by Oracle Corp, provided free of cost.
- Another excellent tutorial is the book Head First Java, 3rd Edition by Kathy Sierra, Bert Bates, Trisha Gee.
Improving that code
I would make some changes to that code.
I would mark the method parameter as final to ensure that in the method body no other reference is assigned to numbers.
The passed argument should be validated, to be sure the calling programmer did not pass a null. A handy way to make this check is a call to Objects.requireNonNull.
We needn’t repeat the Integer as the type of the list in the second line. The compiler in modern Java can deduce that type. So just use <>.
Generally it is best to use the highest possible superclass or superinterface that offers the needed functionality. The superinterface of ArrayList is List. In Java 21 and later, an even higher superinterface is SequencedCollection (see JEP 431).
I am one of those folks who thinks it generally best to return an unmodifiable collection. So after populating the ArrayList, I would generate a new unmodifiable List by calling List.copyOf.
I would let the text of the code breathe, adding some SPACE characters. The crammed-together style is a historical artifact dating back to the 80-column limits of punched cards and terminals.
public SequencedCollection < Integer > onlyPrimes( final int[] numbers ) {
Objects.requireNonNull( numbers ) ;
SequencedCollection < Integer > primes = new ArrayList<>();
…
return List.copyOf( primes ) ;
}
Answer from Basil Bourque on Stack OverflowUsing methods from an ArrayList Java - Stack Overflow
Putting methods into an ArrayList Java - Stack Overflow
How do I create a 2d ArrayList in java?
MOOC Intro Java Course Exercise 62 (Using methods to remove items from an ArrayList): MOOC gave me a void type method to do the removing; why don't I need a return value?
Post your code so it's easier to understand what you're talking about.
More on reddit.comVideos
I assume you are doing something like this (sorry but it is not clear from your question):
ArrayList<SuperClass> al;
...populated with instances of Appliance
al.get(1).getClock(); //Compile error.
The problem is that java does not know if your element at that position is a SuperClass, Appliance, or something else that inherits from SuperClass. You can cast the code, to make it behave the way you want:
((Appliance)al.get(1)).getClock();
You may also want to use the instanceOf operator to make sure you do have an instance of the class you are expecting.
It sounds like you're writing
List<Appliance> appliances = new ArrayList<>();
appliances.add(new Lamp());
appliances.add(new Clock());
Appliance appliance = appliances.get(0);
appliance.setAlarm(TOMORROW);
I think from this example you can see why you can't access the subclass methods. When you have a list of Appliance, you don't know if the objects in it are Clocks or Lamps, so when you get one out you can't call the subclass methods.
If you are positive the object is a clock, you could cast it:
Clock clock = (Clock) appliances.get(1);
clock.setAlarm(TOMORROW);
But this is not the Java Way. Typically you would use only superclass methods on Appliance, or maintain a separate list for Clocks and Lamps.