#javaarray
Explore tagged Tumblr posts
arshikasingh · 10 months ago
Text
Java Array
Let us see the definition of Java Array:
Tumblr media
3 notes · View notes
java-highlight · 27 days ago
Text
Mảng (Array) Trong Java | Cách Dùng & Ví Dụ
Mảng (Array) trong Java là một cấu trúc dữ liệu quan trọng, được sử dụng để lưu trữ nhiều giá trị cùng kiểu dữ liệu trong một biến duy nhất. Với tính chất đơn giản nhưng mạnh mẽ, mảng giúp lập trình viên quản lý dữ liệu hiệu quả trong các ứng dụng Java. Trong bài viết này, chúng ta sẽ tìm hiểu mảng trong Java là gì, cách sử dụng và các ví dụ minh họa cụ thể. Bài viết được tối ưu chuẩn SEO, cung cấp thông tin chi tiết và dễ hiểu cho cả người mới bắt đầu và lập trình viên có kinh nghiệm.
Tumblr media
Ảnh mô tả chỉ mục và các phần tử trong một mảng.
Mảng (Array) trong Java là gì?
Mảng trong Java là một tập hợp các phần tử có cùng kiểu dữ liệu, được lưu trữ liên tiếp trong bộ nhớ. Mỗi phần tử trong mảng được truy cập thông qua chỉ số (index), bắt đầu từ 0. Mảng có kích thước cố định, nghĩa là sau khi khởi tạo, bạn không thể thay đổi số lượng phần tử.
Ví dụ, nếu bạn muốn lưu trữ điểm số của 5 học sinh, thay vì tạo 5 biến riêng lẻ, bạn có thể sử dụng một mảng để lưu trữ tất cả trong một biến duy nhất.
Đặc điểm chính của mảng trong Java:
Kiểu dữ liệu đồng nhất: Tất cả phần tử trong mảng phải cùng kiểu (int, String, double, v.v.).
Kích thước cố định: Không thể thay đổi kích thước sau khi khởi tạo.
Truy cập nhanh: Nhờ chỉ số, việc truy cập phần tử trong mảng rất nhanh.
Cách khai báo và khởi tạo mảng trong Java
1. Khai báo mảng
Để sử dụng mảng trong Java, bạn cần khai báo trước. Cú pháp khai báo như sau:kiểu_dữ_liệu[] tên_mảng;
Ví dụ: int[] numbers; // Khai báo mảng số nguyên String[] names; // Khai báo mảng chuỗi
2. Khởi tạo mảng
Sau khi khai báo, bạn cần khởi tạo mảng với kích thước cụ thể hoặc gán giá trị trực tiếp.
Khởi tạo với kích thước: numbers = new int[5]; // Mảng 5 phần tử, mặc định giá trị là 0
Khởi tạo với giá trị: int[] numbers = {10, 20, 30, 40, 50};
Tumblr media
Minh họa cú pháp khai báo và khởi tạo mảng
Cách truy cập và thao tác với mảng
1. Truy cập phần tử mảng
Mỗi phần tử trong mảng được truy cập thông qua chỉ số (index). Ví dụ: int[] numbers = {10, 20, 30, 40, 50}; System.out.println(numbers[0]); // In ra 10 System.out.println(numbers[2]); // In ra 30
2. Thay đổi giá trị phần tử
Bạn có thể gán giá trị mới cho phần tử trong mảng bằng cách sử dụng chỉ số: numbers[1] = 25; // Thay đổi giá trị tại index 1 thành 25
3. Duyệt mảng
Có nhiều cách để duyệt qua các phần tử của mảng trong Java:
Sử dụng vòng lặp for:
for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }
Sử dụng vòng lặp for-each:
for (int num : numbers) { System.out.println(num); }
Tumblr media
Vòng lặp for  truy cập các phần tử trong mảng
Các ví dụ thực tế về mảng trong Java
Ví dụ 1: Tính tổng các phần tử trong mảng
public class SumArray { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; int sum = 0; for (int num : numbers) { sum += num; } System.out.println("Tổng các phần tử: " + sum); } }
Kết quả: Tổng các phần tử: 150
Ví dụ 2: Tìm phần tử lớn nhất trong mảng
public class MaxArray { public static void main(String[] args) { int[] numbers = {5, 2, 8, 1, 9}; int max = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; } } System.out.println("Phần tử lớn nhất: " + max); } }
Kết quả: Phần tử lớn nhất: 9
Một số lưu ý khi sử dụng mảng trong Java
Lỗi ArrayIndexOutOfBoundsException: Xảy ra khi bạn truy cập chỉ số ngoài phạm vi của mảng. Ví dụ: truy cập numbers[5] trong mảng có kích thước 5 sẽ gây lỗi.
Kích thước cố định: Nếu cần mảng có kích thước động, bạn nên sử dụng ArrayList thay vì mảng.
Hiệu suất: Mảng rất nhanh trong việc truy cập phần tử, nhưng không phù hợp khi cần thêm/xóa phần tử thường xuyên.
Tumblr media
Lỗi ArrayIndexOutOfBoundsException
Mảng đa chiều trong Java
Mảng không chỉ giới hạn ở một chiều. Mảng đa chiều (2D, 3D,...) thường được sử dụng để biểu diễn ma trận hoặc bảng dữ liệu. Ví dụ về mảng 2 chiều: int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; System.out.println(matrix[1][2]); // In ra 6
Tumblr media
Hình ảnh minh họa mảng 2 chiều
Kết luận
Mảng (Array) trong Java là một công cụ mạnh mẽ để lưu trữ và xử lý dữ liệu. Với cú pháp đơn giản, mảng phù hợp cho nhiều bài toán lập trình, từ cơ bản đến nâng cao. Bằng cách nắm vững cách khai báo, khởi tạo, truy cập và thao tác với mảng, bạn sẽ dễ dàng giải quyết các vấn đề liên quan đến dữ liệu. Hy vọng bài viết này đã cung cấp cho bạn cái nhìn tổng quan và những ví dụ thực tế hữu ích về mảng trong Java.
Nếu bạn muốn tìm hiểu thêm về mảng hoặc các cấu trúc dữ liệu khác trong Java, hãy tiếp tục khám phá và thực hành qua các bài tập thực tế!
🔹 Mảng (Array) Trong Java | Cách Dùng & Ví Dụ Dễ Hiểu Tìm hiểu cách khai báo, truy cập và sử dụng mảng trong Java. Hướng dẫn kèm ví dụ thực tế giúp bạn nắm vững kiến thức nhanh chóng. 🌐 Website: Java Highlight
0 notes
siva3155 · 6 years ago
Text
300+ TOP JAVASCRIPT Objective Questions and Answers
JavaScript Multiple Choice Questions :-
1.  Why so JavaScript and Java have similar name? A.  JavaScript is a stripped-down version of Java B.  JavaScript's syntax is loosely based on Java's C.  They both originated on the island of Java D.  None of the above Ans: B 2.  When a user views a page containing a JavaScript program, which machine actually executes the script? A.  The User's machine running a Web browser B.   The Web server C.  A central machine deep within Netscape's corporate offices D.  None of the above Ans:  A 3.  ______ JavaScript is also called client-side JavaScript. A.  Microsoft B.  Navigator C.  LiveWire D.  Native Ans: B 4.  __________ JavaScript is also called server-side JavaScript. A.  Microsoft B.   Navigator C.  LiveWire D.  Native Ans: C 5.  What are variables used for in JavaScript Programs? A.  Storing numbers, dates, or other values B.   Varying randomly C.  Causing high-school algebra flashbacks D.  None of the above Ans: A 6.  _____ JavaScript statements embedded in an HTML page can respond to user events such as mouse-clicks, form input, and page navigation. A.  Client-side B.   Server-side C.  Local D.  Native Ans: A 7.  What should appear at the very end of your JavaScript? The tag A.   The B.    The C.  The END statement D.  None of the above Ans: A 8.  Which of the following can't be done with client-side JavaScript? A.  Validating a form B.   Sending a form's contents by email C.  Storing the form's contents to a database file on the server D.  None of the above  Ans: C 9.  Which of the following are capabilities of functions in JavaScript? A.  Return a value B.   Accept parameters and Return a value C.  Accept parameters D.  None of the above Ans: C 10.  Which of the following is not a valid JavaScript variable name? A.  2names B.   _first_and_last_names C.  FirstAndLast D.  None of the above  Ans: A
Tumblr media
JAVASCRIPT Objective Questions 11.  ______ tag is an extension to HTML that can enclose any number of JavaScript statements. A.   B.   C.   D.   Ans: A 12.  How does JavaScript store dates in a date object? A.  The number of milliseconds since January 1st, 1970 B.   The number of days since January 1st, 1900 C.  The number of seconds since Netscape's public stock offering. D.  None of the above Ans: A 13.  Which of the following attribute can hold the JavaScript version? A.  LANGUAGE B.   SCRIPT C.  VERSION D.  None of the above  Ans: A 14.  What is the correct JavaScript syntax to write "Hello World"? A.  System.out.println("Hello World") B.   println ("Hello World") C.  document.write("Hello World") D.  response.write("Hello World") Ans: C 15.  Which of the following way can be used to indicate the LANGUAGE attribute? A.   B.   C.      JavaScript statements… D.      JavaScript statements… Ans: C 16.  Inside which HTML element do we put the JavaScript? A.   B.   C.   D.   Ans: C 17.  What is the correct syntax for referring to an external script called " abc.js"? A.   B.   C.   D.  None of the above Ans: C 18.  Which types of image maps can be used with JavaScript? A.  Server-side image maps B.  Client-side image maps C.  Server-side image maps and Client-side image maps D.  None of the above Ans: B 19.  Which of the following navigator object properties is the same in both   Netscape and IE? A.  navigator.appCodeName B.   navigator.appName C.  navigator.appVersion D.  None of the above  Ans: A 20.  Which is the correct way to write a JavaScript array? A.  var txt = new Array(1:"tim",2:"kim",3:"jim") B.   var txt = new Array:1=("tim")2=("kim")3=("jim") C.  var txt = new Array("tim","kim","jim") D.  var txt = new Array="tim","kim","jim" Ans: C 21.  What does the tag do? A.  Enclose text to be displayed by non-JavaScript browsers. B.   Prevents scripts on the page from executing. C.  Describes certain low-budget movies. D.  None of the above  Ans: A 22. If para1 is the DOM object for a paragraph, what is the correct syntax to change the text within the paragraph? A.  "New Text"? B.  para1.value="New Text"; C.  para1.firstChild.nodeValue= "New Text"; D.  para1.nodeValue="New Text"; Ans: B 23.  JavaScript entities start with _______ and end with _________. A.  Semicolon, colon B.   Semicolon, Ampersand C.  Ampersand, colon D.  Ampersand, semicolon Ans: D 24.  Which of the following best describes JavaScript? A.  a low-level programming language. B.   a scripting language precompiled in the browser. C.  a compiled scripting language. D.  an object-oriented scripting language. Ans: D 25.  Choose the server-side JavaScript object? A.  FileUpLoad B.   Function C.  File D.  Date Ans: C 26.  Choose the client-side JavaScript object? A.  Database B.   Cursor C.  Client D.  FileUpLoad Ans: D 27.  Which of the following is not considered a JavaScript operator? A.  new B.  this C.  delete D.  typeof Ans: B 28.  ______method evaluates a string of JavaScript code in the context of the specified object. A.  Eval B.   ParseInt C.  ParseFloat D.  Efloat Ans: A 29.  Which of the following event fires when the form element loses the focus: , , , , ? A.  onfocus B.  onblur C.  onclick D.  ondblclick Ans: B 30.  The syntax of Eval is ________________ A.  eval(numeriC. B.  eval(string) C.  eval(string) D.  eval(numeriC. Ans: B JAVASCRIPT Objective Type Questions with Answers 31.  JavaScript is interpreted by _________ A.  Client B.   Server C.  Object D.  None of the above Ans: A 32.  Using _______ statement is how you test for a specific condition. A.  Select B.  If C.  Switch D.  For  Ans: B 33.  Which of the following is the structure of an if statement? A.  if (conditional expression is true) thenexecute this codeend if B.   if (conditional expression is true)execute this codeend if C.  if (conditional expression is true)   {then execute this code>->} D.  if (conditional expression is true) then {execute this code} Ans: C 34.  How to create a Date object in JavaScript? A.  dateObjectName = new Date() B.   dateObjectName.new Date() C.  dateObjectName := new Date() D.  dateObjectName Date()  Ans: A 35.  The _______ method of an Array object adds and/or removes elements from an array. A.  Reverse B.   Shift C.  Slice D.  Splice Ans: D 36.  To set up the window to capture all Click events, we use which of the following statement? A.  window.captureEvents(Event.CLICK); B.   window.handleEvents (Event.CLICK); C.  window.routeEvents(Event.CLICK ); D.  window.raiseEvents(Event.CLICK ); Ans: A 37.  Which tag(s) can handle mouse events in Netscape? A.   B.   C.   D.  None of the above  Ans: B 38.  ____________ is the tainted property of a window object. A.  Pathname B.   Protocol C.  Defaultstatus D.  Host Ans: C 39.  To enable data tainting, the end user sets the _________ environment variable. A.  ENABLE_TAINT B.   MS_ENABLE_TAINT C.  NS_ENABLE_TAINT D.  ENABLE_TAINT_NS  Ans: C 40.  In JavaScript, _________ is an object of the target language data type that encloses an object of the source language. A.  a wrapper B.   a link C.  a cursor D.  a form  Ans: A 41.  When a JavaScript object is sent to Java, the runtime engine creates a Java wrapper of type ___________ A.  ScriptObject B.  JSObject C.  JavaObject D.  Jobject Ans: B 42.  _______ class provides an interface for invoking JavaScript methods and examining JavaScript properties. A.  ScriptObject B.  JSObject C.  JavaObject D.  Jobject  Ans: B 43.  _________ is a wrapped Java array, accessed from within JavaScript code. A.  JavaArray B.   JavaClass C.  JavaObject D.  JavaPackage Ans:  A 44. A ________ object is a reference to one of the classes in a Java package, such as netscape.javascript . A.  JavaArray B.  JavaClass C.  JavaObject D.  JavaPackage  Ans: B 45.  The JavaScript exception is available to the Java code as an instance of __________ A.  netscape.javascript.JSObject B.  netscape.javascript.JSException C.  netscape.plugin.JSException D.  None of the above Ans: B 46. To automatically open the console when a JavaScript error occurs which of the following is added to prefs.js? A.  user_pref(" javascript.console.open_on_error", false); B.   user_pref("javascript.console.open_error ", true); C.  user_pref("javascript.console.open_error ", false); D.   user_pref("javascript.console.open_on_error", true); Ans: D 47.  To open a dialog box each time an error occurs, which of the following is added to prefs.js? A.  user_pref("javascript.classic.error_alerts", true); B.   user_pref("javascript.classic.error_alerts ", false); C.  user_pref("javascript.console.open_on_error ", true); D.  user_pref("javascript.console.open_on_error ", false);  Ans: A 48.  The syntax of a blur method in a button object is ______________ A.  Blur() B.   Blur(contrast) C.  Blur(value) D.  Blur(depth)  Ans: A 49.  The syntax of capture events method for document object is ______________ A.  captureEvents() B.   captureEvents(args eventType) C.  captureEvents(eventType) D.  captureEvents(eventVal)  Ans: C 50.  The syntax of close method for document object is ______________ A.  Close(do). B.   Close(object) C.  Close(val) D.  Close() Ans: D 51. x=4+"4"; document.write(x); Output------? A. 44 B. 8 C. 4 D. Error output Ans: A 52. Is it possible to nest functions in JavaScript? A. True B. False Ans: A 53. document.write(navigator.appCodeName); A. get code name of the browser of a visitor B. set code name of the browser of a visitor C. None of the above Ans: A 54. Scripting language are A. High Level Programming language B. Assembly Level programming language C. Machine level programming language Ans: A 55. Which best explains getSelection()? A. Returns the VALUE of a selected OPTION. B. Returns document.URL of the window in focus. C. Returns the value of cursor-selected text D. Returns the VALUE of a checked radio input. Ans: C 56. Choose the client-side JavaScript object: A. Database B. Cursor C. Client D. FileUpLoad Ans: D 57. What is mean by "this" keyword in javascript? A. It refers current object B. It referes previous object C. It is variable which contains value D. None of the above Ans: A 58. In JavaScript, Window.prompt() method return true or false value ? A. False B. True Ans: A 59. function x() { document.write(2+5+"8"); } A. 258 B. Error C. 7 D. 78 Ans: D 60. var s = "9123456 or 80000?"; var pattern = /d{4}/; var output = s.match(pattern); document.write(output); A. 9123 B. 91234 C. 80000 D. None of the above Ans: A JAVASCRIPT Questions and Answers pdf Download Read the full article
0 notes
fabricadeconhecimentos · 6 years ago
Text
Crie 15 Aplicativos Completos com Android Studio e Java
Tumblr media
Curso Completo do Básico até o nível mais Avançado! Já pensou em se especializar em desenvolvimento Android? Que tal ser um dos profissionais mais cobiçados do mercado?O curso é todo desenvolvido baseado em projetos reais. Vamos construir aplicativos clones de grandes apps famosos. Começaremos baixando o Android Studio e configurando o nosso emulador. Em seguida, vamos entender como funcionam as views, os layouts, a linguagem java e como utilizar a lógica de programação em seus projetos.Criaremos um clone do Twitter utilizando o Parse e em seguida, faremos o 7 Minutes Workout integrado com o Youtube. Vamos criar nosso clone do Tinder e descobrir como utilizar o sistema de login do Facebook. Em seguida, você vai criar um clone do Trip Advisor e aprender como trabalhar com o Google Maps e o banco de dados SQLite. E finalmente vamos criar o FlappyBird, utilizando a engine de games GDX e sprites.Tudo isso para depois aprender a publicar seu App na Google Play.Se você tiver dúvidas ao longo do curso, não se preocupe. Estaremos nos fóruns respondendo as suas perguntas!E aí? O que tá esperando pra começar a criar seus aplicativos? Inscreva-se agora mesmo e comece a criar aplicativos incríveis com o Android Studio!Principais assuntos que aprenderemos neste curso:Android Studio 3.2.1 - Download e InstalaçãoLógica de Programação com JavaArrays, Arraylist e Classes de ObjetosTrabalhar com arquivos JSONMaterial DesignRecyclerViewYouTubeAPI e PicassoBanco de Dados Firebase e SQLComo Publicar o app na Google Play15 Aplicativos Completos que criaremos passo a passo:Contador de pessoasAlfândegaJo Kem PoCalculadoraQuizzLista de Cartões do FelpudoImagens dos PersonagensLista de TarefasAgenda de ContatosShow do MilhãoTreino (7 Minutes Workout)Trip AdvisorTwitterTinderFlappyBirdVantagens de se tornar um alunoAcesso vitalício ao curso: assista quando e quantas vezes quiser.Download dos vídeos: através dos Apps Udemy para Android/iOS você pode fazer download dos vídeos e assistí-los offline.Certificado reconhecido: a Udemy oferece certificação de conclusão do curso, especificando o curso e a carga horária realizada. Who this course is for: Qualquer pessoa interessada em aprender mais ou se aprofundar no desenvolvimento de aplicativos Android Read the full article
0 notes
rajtechtrainer-blog · 6 years ago
Video
youtube
Core Java for Test Engineers : Arrays in Java.In this session we go through examples on Simple,Jagged Arrays and also look at the advanced for loop in Java.Don't forget to subscribe to the channel
0 notes
arshikasingh · 11 months ago
Text
Example of Printing Java Array using the "for-each" Loop
Let us see an example of printing Java array using the "for-each" loop
Tumblr media
2 notes · View notes
arshikasingh · 1 year ago
Text
Example of Printing Java Array using the "for" Loop
Let us see an example of printing Java array using the "for" loop
Tumblr media
2 notes · View notes
arshikasingh · 10 months ago
Text
Advantages of Java Array
Let us see the advantages of Java Array:
Tumblr media
1 note · View note