How to find the largest number and its position within an array in Java

This is a basic problem when working with arrays in Java. The idea is to find the largest number of the array and the position it occupies.

The first thing is to declare the array of numbers and initialize it with any sequence:

int [] listNumbers = {1,4,7,8,9,2,7,15,4,6,9,8};
The following, declare two variables, in which we will store the largest number and its position.

To begin with, we will assume that the largest number is the first and that consequently its position is zero (remember that the arrays begin with the 0 position).

int iMajor Number, iPosicion;

// We assume that the largest number is the first
MajorNumber = listNumber [0];
iPosicion = 0;
Now, we will go through the entire array. For this we use a repetitive structure type for and the property .length of the array, which gives us the number of elements stored by the array.

for (int x = 1; x <listNumbers.length; x ++) {…}
For each element of the array that we go through, we must ask if that element is greater than the one we have stored. If this is true we will put this as the new major number and the iteration variable of the loop as position.

if (listaNumeros [x]> iNumeroMayor) {
MajorNumber = listNumbers [x];
iPosicion = x;
}
We only have to show the values ​​of the Highest Number and iPosition. Simple, simple and in a few lines.

जावा में एरे के साथ काम करते समय यह एक बुनियादी समस्या है विचार यह है कि सरणी की सबसे बड़ी संख्या और उस स्थान पर कब्जा कर लिया गया स्थान।

पहली बात यह है कि संख्याओं की सरणी को घोषित करना और इसे किसी क्रम के साथ प्रारंभ करना है:

int [] सूची अंक = {1,4,7,8,9,2,7,15,4,6,9,8};
निम्नलिखित, दो चर घोषित करें, जिसमें हम सबसे बड़ी संख्या और उसकी स्थिति को स्टोर करेंगे।

आरंभ करने के लिए, हम मान लेंगे कि सबसे बड़ी संख्या सबसे पहले है और इसके परिणामस्वरूप इसकी स्थिति शून्य है (याद रखें कि सरणियों की स्थिति 0 से शुरू होती है)।

आईआईएमएजर संख्या, आईपीसीसीयन;

// हम मानते हैं कि सबसे बड़ी संख्या सबसे पहले है
MajorNumber = listNumber [0];
आईपोसिसियन = 0;
अब, हम पूरी सरणी के माध्यम से जाएंगे। इसके लिए हम दोहराए जाने वाले संरचना प्रकार और संपत्ति का उपयोग करते हैं। सरणी के लम्बाई, जो हमें सरणी द्वारा संग्रहित तत्वों की संख्या देता है।

के लिए (int x = 1; x <listNumbers.length; x ++) {…}
जिस सरणी के माध्यम से हम जाते हैं, हमें पूछना चाहिए कि वह तत्व हमारे द्वारा संग्रहीत किसी से अधिक है। अगर यह सत्य है तो हम इसे लूप की स्थिति के रूप में नई प्रमुख संख्या और चलने वाले चर के रूप में रखेंगे।

अगर (listaNumeros [x]> iNumeroMayor) {
MajorNumber = listNumbers [x];
आईपोसिसियन = एक्स;
}
हमें सर्वोच्चतम संख्या और आइपीओशन के मूल्यों को ही दिखाना होगा सरल, सरल और कुछ पंक्तियों में

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!