What is the Java varargs (...) parameter data type?

I am trying to add information from main () to an element class where I store information in hashset

i has 3 classes

  • project - main ()

  • libaray - addBandMembers function

  • Item - addband (String ... member)

I am adding CD information. firstly, I add a group, # songs, the name works well

then in another function I'm trying to add group members. I want to keep them separate. Where I have problems, we assign group members.

I understand how Varargs works, just not sure how to assign this to a string.

I will only show bits of code to keep this post simple and short.

this is what i have:

Main ()

item = library.addMusicCD("Don't Let Go", "Jerry Garcia Band", 15, "acid rock", "jam bands");
if (item != null) {
library.addBandMembers(item, "Jerry Garcia", "Keith Godcheaux");
library.printItem(out, item);
}

Then here is the first function called ...

public void addBandMembers(Item musicCD, String... members)
{

   //musicCD.addband(members);   // both cant find addband function..
    //Item.addband(members);

}

Then in another class I try to add information.

 private String members;  


public void addband(String... member)
{
    this.members = member;     // error

}

String; .. ..

 public String getMembers()
{
 return members;   
}

?

oh ya, .

public class Library
{
private Set<CD> theCDs = new HashSet<CD>();
private Set<DVD> theDVDs = new HashSet<DVD>();
private Set<Book> theBooks = new HashSet<Book>();

, .. .

+3
3

A varargs array.

,

private String members;  

private String[] members;  
+2

, String .

+1

String ... member gives you an array, so what you are looking at is a member of type String [].

I think, as soon as you find out, you will sort of cope with the problem.

+1
source

Source: https://habr.com/ru/post/1733283/


All Articles