Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of traversing JAVA Array

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the JAVA array traversal example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

/ *

Case study of implementing inventory management:

1. Store commodity information

Store commodity type variables

Store variables of commodity types in the collection

2. View inventory

Traverse the collection to get the Goods type variables stored in the collection

Output the properties of each Goods type

Calculate the sum: total inventory, total amount

3. Modify the inventory of goods

Traversing the collection to get the Goods type variables stored in the collection

Variable calls the property count of the Goods class, and the value is modified (keyboard input)

, /

/ / import java.util.ArrayList

Import java.util.*

Public class Shopp {

Public static void main (String [] args) {

/ / create an ArrayList collection to store Goods types

ArrayList array = new ArrayList ()

/ / call the method of adding commodity information

AddGoods (array)

/ / enter into an endless cycle

While (true) {

/ / call the method of selecting a function to obtain the function serial number entered by the user

Int number = chooseFunction ()

/ / A pair of serial numbers are judged, if = 1 enters the view inventory function = 2 enters the modify inventory function = 3 ends

Switch (number) {

Case 1:

/ / enter to view inventory, call the method to view inventory, and pass a collection of stored product information

PrintStore (array)

Break

Case 2:

/ / enter the function of modifying inventory, call the method of modifying inventory, and pass the collection

Update (array)

Break

Case 3:

Return

Default:

System.out.println ("without this feature")

Break

}

}

}

/ *

Method definition, modify inventory

Keyboard input, change the property values in Goods, modify

, /

Public static void update (ArrayList array) {

Scanner sc = new Scanner (System.in)

/ / iterate through the collection to get each element in the collection

For (int I = 0; I < array.size (); iTunes +) {

/ / the collection method get gets the element of the collection. The element type is Goods.

Goods g = array.get (I)

System.out.println ("Please enter the inventory number of" + g.brand+ ")

/ / Goods attribute, which is modified by count

G.count = sc.nextInt ()

}

}

/ *

Define the method, realize the selection menu, and the user selects the menu according to the function.

, /

Public static int chooseFunction () {

System.out.println ("- inventory management -")

System.out.println ("1. View inventory")

System.out.println ("2. Modify inventory quantity")

System.out.println ("3. Quit")

System.out.println ("Please enter the operation number to be performed:")

Scanner sc = new Scanner (System.in)

Int number = sc.nextInt ()

Return number

}

/ *

Define methods, view inventory, traverse collections

, /

Public static void printStore (ArrayList array) {

/ / output header

System.out.println ("- Mall inventory -")

System.out.println ("Brand Model size Price inventory")

/ / define variables, save total inventory, and total amount

Int totalCount = 0

Double totalMoney = 0

/ / traverse the collection

For (int I = 0; I < array.size (); iTunes +) {

/ / get (index) gets the elements in the collection, stores the Goods class, and also gets the Goods type

/ / use Goods type variable to accept the result of get method

Goods g = array.get (I)

System.out.println (g.brand + "" + g.size+ "" + g.price+ "" + g.count)

TotalCount = totalCount+g.count

TotalMoney = totalMoney + g.count*g.price

}

System.out.println ("Total inventory:" + totalCount)

System.out.println ("Total inventory amount:" + totalMoney)

}

/ *

Define a method to store information about items in a collection

The collection is the shared data of all methods, and parameters are passed.

, /

Public static void addGoods (ArrayList array) {

/ / create a commodity type variable, a Goods type variable

Goods G1 = new Goods ()

Goods G2 = new Goods ()

G1.brand = "MacBook"

G1.size = 13.3

G1.price = 9999.99

G1.count = 3

G2.brand = "Thinkpad"

G2.size = 15.6

G2.price = 7999.99

G2.count = 1

/ / variables of type Goods, stored in the collection

Array.add (G1)

Array.add (G2)

}

}

Thank you for reading this article carefully. I hope the article "sample Analysis of JAVA Array traversal" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report