Java List Query Example: Finding a Target Number
Sure! Here is an example of Java code for performing a list query:\n\njava\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class ListQueryExample {\n public static void main(String[] args) {\n // Create a sample list of integers\n List<Integer> numbers = new ArrayList<>();\n numbers.add(10);\n numbers.add(20);\n numbers.add(30);\n numbers.add(40);\n numbers.add(50);\n\n // Perform list query\n int target = 30;\n boolean found = false;\n int index = -1;\n\n for (int i = 0; i < numbers.size(); i++) {\n if (numbers.get(i) == target) {\n found = true;\n index = i;\n break;\n }\n }\n\n // Display the query result\n if (found) {\n System.out.println("The target number " + target + " was found at index " + index);\n } else {\n System.out.println("The target number " + target + " was not found in the list");\n }\n }\n}\n\n\nIn this example, we create a list of integers and perform a query to find a specific target number (30 in this case). The code iterates through the list using a for loop and checks if each element matches the target. If a match is found, the loop breaks and the index of the match is stored. Finally, the code displays the result based on whether the target number was found or not.
原文地址: https://www.cveoy.top/t/topic/pE78 著作权归作者所有。请勿转载和采集!