/    Sign up×
Community /Pin to ProfileBookmark

Can someone explain to me what is this :

[code] public Knapsack[b](Item[] items)[/b]
[/code]

then the use of that is like this Knapsack k = new Item(“Pen”, 15)

how do I get only those two arguments that are passing in a different class as arguments so I can use only PEN and only number 15 separately in my constructor?

to post a comment
Java

1 Comments(s)

Copy linkTweet thisAlerts:
@resolvedpromiseOct 21.2018 — It looks like a constructor to create a Knapsack object from an array of Item objects.

First, you need to create an array of Item objects:

``<i>
</i>Item[] items = new Item[2];<i>
</i>
`</CODE>
This declares a variable named <C>
items</C> that references an array of 2 item objects (that don't exist, yet).

Then you can put Item objects in the array with:
<CODE>
`<i>
</i>items[0] = new Item("Pen", 15);
items[1] = new Item("Paper", 30);<i>
</i>
`</CODE>
Now, you can create a Knapsack object with the array of items:
<CODE>
`<i>
</i>Knapsack myKnapsack = new Knapsack(items);<i>
</i>
``
×

Success!

Help @marcellio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...